Jquery SlideToggle 在停止调用后未完全打开
我在使用 JQuery 切换动画时遇到问题。我有一个菜单,其中有子项目,可在主项目悬停时滑动打开。我可以很好地实现这一点,但是,对于一长串项目,我希望能够停止打开并滑动关闭菜单,而不必等待 SlideDown() 动画完成。
$('.menubar-topitem').on('mouseenter',function(){
$(this).children('.menubar-inner').stop(true).slideDown();
}).on('mouseleave',function(){
$(this).children('.menubar-inner').stop(true).slideUp();
});
这段代码实现了我的要求,但是当我将鼠标悬停在菜单项上时,子菜单仅在我第一次停止动画之前打开。看来 JQuery 存储了动画第一次停止时的子菜单高度,然后第二次才打开到这个高度。
我知道我可以通过使用 stop(true,true) 来解决这个问题,但这会在动画中产生可怕的跳跃。无论如何,我是否可以阻止这种行为,而无需完成动画或任何解决方法,以便用户看不到任何跳转,而是看到流动的菜单系统?
I have an issue with a toggling animation with JQuery. I have a menu, which has subitems, which slide open on hover of the main item. This I can achieve fine, however, for a long list of items, I would prefer to be able to stop the opening, and slide shut the menu, without having to wait for the slideDown() animation to complete.
$('.menubar-topitem').on('mouseenter',function(){
$(this).children('.menubar-inner').stop(true).slideDown();
}).on('mouseleave',function(){
$(this).children('.menubar-inner').stop(true).slideUp();
});
This code achieves what I require, however when I then hover back over the menu item, the submenu only opens as far as the animation reached before I stopped it the first time. It seems that JQuery stores the height of the submenu from when the animation was stopped first time, and then only opens as far as this the second time.
I am aware that I can fix this problem by using stop(true,true) but that creates a horrible jump in the animation. Is there anyway I can prevent this behaviour without having to complete the animation, or any work around so that the user does not see any jumps, but instead sees a flowing menu system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的方法是显式对硬编码值进行动画处理,而不是依赖
slideUp
/slideDown
函数:然后将 CSS
overflow
属性设置为动画元素的隐藏
。另外,要动态获取.menubar-inner
元素的高度,您可以在其中嵌套div
并获取该div
元素的高度:HTML--
CSS--
这是一个演示: http://jsfiddle.net/n6h2S/
你将运行遇到同样的问题
fadeIn
/fadeOut
但还有另一个名为fadeTo
的函数,它允许您显式设置要淡出的不透明度。The fix for this is to explicitly animate hard-coded values rather than depending on the
slideUp
/slideDown
functions:Then set the CSS
overflow
property tohidden
for the animated elements. Also to dynamically get the height of the.menubar-inner
elements you can nest adiv
in them and get the height of thatdiv
element:HTML--
CSS--
Here is a demo: http://jsfiddle.net/n6h2S/
You will run into the same problem with
fadeIn
/fadeOut
but there is another function calledfadeTo
that allows you to explicitly set the opacity to fade to.