Jquery SlideToggle 在停止调用后未完全打开

发布于 2024-12-24 17:10:36 字数 567 浏览 0 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-12-31 17:10:36

解决此问题的方法是显式对硬编码值进行动画处理,而不是依赖 slideUp/slideDown 函数:

$('.menubar-topitem').on('mouseenter',function(){
    var $this  = $(this),
        height = $this.children('.menubar-inner').children().height();
    $this.children('.menubar-inner').stop().animate({height : height}, 250); 
}).on('mouseleave',function(){
    $(this).children('.menubar-inner').stop().animate({height : 0}, 250);
});

然后将 CSS overflow 属性设置为动画元素的隐藏。另外,要动态获取 .menubar-inner 元素的高度,您可以在其中嵌套 div 并获取该 div 元素的高度:

HTML--

<div class="menubar-topitem">
    <h1>This is a Menu Top Item</h1>
    <div class="menubar-inner">
        <div>
            <p>Some Content</p>
            <p>Some More Content</p>
        </div>
    </div>
</div>

CSS--

.menubar-inner {
    overflow : hidden;
    height   : 0;
}

这是一个演示: 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:

$('.menubar-topitem').on('mouseenter',function(){
    var $this  = $(this),
        height = $this.children('.menubar-inner').children().height();
    $this.children('.menubar-inner').stop().animate({height : height}, 250); 
}).on('mouseleave',function(){
    $(this).children('.menubar-inner').stop().animate({height : 0}, 250);
});

Then set the CSS overflow property to hidden for the animated elements. Also to dynamically get the height of the .menubar-inner elements you can nest a div in them and get the height of that div element:

HTML--

<div class="menubar-topitem">
    <h1>This is a Menu Top Item</h1>
    <div class="menubar-inner">
        <div>
            <p>Some Content</p>
            <p>Some More Content</p>
        </div>
    </div>
</div>

CSS--

.menubar-inner {
    overflow : hidden;
    height   : 0;
}

Here is a demo: http://jsfiddle.net/n6h2S/

You will run into the same problem with fadeIn/fadeOut but there is another function called fadeTo that allows you to explicitly set the opacity to fade to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文