jQuery:如何在没有 jQuery UI 的情况下重现手风琴的滑出效果?
我只是在学习 jQuery,弄清楚什么是可能的,找到我的方法。
我想要一个像手风琴一样“滑出”的 div,但我不想支付 jQuery UI 下载的费用,而且我想允许同时打开多个 div。
它是如何运作的? jquery.ui.accordion.js 内部手风琴的滑出效果是如何完成的?我可以调用 .animate()
将 div 从 display:none
移动到 display:block
吗? (我读到 animate 仅适用于数字属性。)
I'm just learning jQuery, figuring out what's possible, finding my way around.
I'd like to have a div "slide out" like an accordion, but I don't want to pay the cost of the jQuery UI download, and also I want to allow multiple divs to be open at the same time.
How does it work? How is the slide-out effect of the accordion done, inside jquery.ui.accordion.js? Is there a call I can make to .animate()
to move a div from display:none
to display:block
? (I read that animate works only with numeric properties.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以对一个元素执行此操作。
使用slideToggle
http://docs.jquery.com/Effects/slideToggle
但是如果你想做多个级别,您还需要添加一个功能来浏览所有项目,如果您只想一次打开一个项目,则在滑动切换触发之前将它们向上滑动或隐藏它们。这就是 jquery 手风琴可以提供帮助的地方。他们已经加入了许多这样的选项。
You can do this for one element.
Using slideToggle
http://docs.jquery.com/Effects/slideToggle
But if you want to do multiple levels, you will need to also add a function to go through all items and Slide them up or hide them before the slidetoggle fires if you only want one item open at a time. This is where jquery accordion can help. They have wired in many options like this.
如果您要创建垂直手风琴,则可以使用 SlideDown 和 SlideUp,否则,您将需要结合更多 CSS 以允许在某些 div 的定位上使用 animate。
You can use slideDown and slideUp if you are creating a vertical accordion, otherwise, you will need to combine a bit more CSS to allow for use of animate on the positioning of some divs.
仅供参考,jQueryUI 和核心中的缩小幻灯片效果在第一个请求中只有 5.5KB。不需要整个 jQueryUI 库。您需要的文件 - ui.slider.min.js 和 ui.core.min.js
Just FYI, the minified slide effect in jQueryUI and core would be only a 5.5KB hit on the first request. There'd be no need for the entire jQueryUI library. Files you'd need - ui.slider.min.js and ui.core.min.js
我正在学习各种各样的东西。我只使用了 jQuery - 没有 UI,没有 UI Accordion,没有 UI Effects - 并得到了我想要的下滑效果。不确定这种视觉效果的术语。 (我认为这是 jQuery 挑战的一半 - 我如何称呼各种效果)。我想也许有些人称之为“切换百叶窗”。
点击这里查看效果: http://jsbin.com/ogape
这是 html 片段
这是 jQuery代码:
slideOut() 函数与其实际功能相比略有简化。它还将实际代码中的 + 和 - 替换为扩展器按钮,这使得它变得更加复杂:
您可以在上面引用的实时演示链接中看到完整的 html/js 代码。
我不知道的关键是,可以通过使用“toggle”调用 animate 来对高度进行动画处理。
I'm learning all sorts of things. I used jQuery only - no UI, no UI Accordion, no UI Effects - and got the slide-down effect that I wanted. Not sure of the terminology for this visual effect. (I think that is half the challenge with jQuery - what do I call the various effects). I think maybe some people call this "toggle blinds".
Click this to see the effect: http://jsbin.com/ogape
Here's the html fragment
This is the jQuery code:
That slideOut() function is slightly simplified from what it actually does. It also swaps the + and - for the expander button in the actual code, which makes it a little more complicated:
You can see the full html/js code at the live demo link referenced above.
The key thing that I did not know, is that one can animate the height by calling animate with "toggle".