简体   繁体   English

jquery 在 hover 上使用多个列表项进行动画处理

[英]jquery animate on hover with multiple list-items

OK, I've got my desired animation to be very close to what I want;好的,我已经得到了我想要的 animation 非常接近我想要的; however I'm running into a bug.但是我遇到了一个错误。 http://garibaldi.juggernautwebdesign.com/ and it's the #menu-left nav (colourful one). http://garibaldi.juggernautwebdesign.com/这是#menu-left nav(彩色的)。 If you hover, it expands open like it's supposed to.如果你是 hover,它会像预期的那样展开。 And if you let it fully animate , then hover over a new element OR hover off that list-item, it then closes, properly and a new item will open (if you hover over a new list-item).如果你让它完全动画,然后 hover 超过一个新元素或 hover 关闭该列表项,然后它会正确关闭并打开一个新项目(如果你 hover 超过新列表项) However, if you hover over a list-item and then quickly hover off before it has time to fully animate, then it slides back down too far.但是,如果您在列表项上使用 hover,然后在它有时间完全动画之前快速关闭 hover,那么它会滑回太远。 Is there any way to stop any animations from happening, until the initial hover is fully animated?有什么方法可以阻止任何动画的发生,直到最初的 hover 完全动画化? I tried using the queue option, but that just stopped any animation on a hover out.我尝试使用队列选项,但这只是停止了 hover 上的任何 animation。

Here's my code:这是我的代码:

$(document).ready(function() {  
    //Left nav  
    $('#menu-left').children().addClass('closed');

    $('#menu-left > li a').click(function(e){
        e.preventDefault();
    });

    $('.closed').live('hover', function() { 

        var position_open = parseInt($(this).css('bottom')) + parseInt($('.sub-menu', this).css('height')) - 12;
        $(this).animate({ bottom: position_open }, function() {
             $(this).removeClass('closed'); 
             $(this).addClass('opened');        
        });     
    }); 

    $('.opened').live('hover', function() {
        var position_close = parseInt($(this).css('bottom')) - parseInt($('.sub-menu', this).css('height')) + 12;
        $(this).animate({ bottom: position_close }, function() {
            $(this).removeClass('opened');
            $(this).addClass('closed');
        });     
    }); 

});

Any help would be greatly appreciated!任何帮助将不胜感激!

You can try adding a flag before calling animate.您可以在调用 animate 之前尝试添加标志。 Something like this:像这样的东西:

$(document).ready(function() {
var animating = false;
        //Left nav  
        $('#menu-left').children().addClass('closed');

        $('#menu-left > li a').click(function(e){
            e.preventDefault();
        });

        $('.closed').live('hover', function() { 

            var position_open = parseInt($(this).css('bottom')) + parseInt($('.sub-menu', this).css('height')) - 12;
                    if(!animating) { 
                        animating = true;
                $(this).animate({ bottom: position_open }, function() {
                 $(this).removeClass('closed'); 
                 $(this).addClass('opened');
                               animating = false;
                });     
                    }
        });         

    });

Also, try removing one of the hover handler code.此外,尝试删除 hover 处理程序代码之一。 Seems they are stepping over each other.似乎他们正在互相踩踏。

I can't be sure without more testing, but I'm betting your events aren't bubbling the way you'd expect.如果没有更多的测试,我无法确定,但我打赌你的事件不会像你期望的那样冒泡。 You're adding/removing the class while you're still hovering/animating, so the process fires a few times.您在悬停/动画时添加/删除 class,因此该过程会触发几次。

I would suggest rewriting the function to trigger off of mouseenter/mouseleave events, I think this would help you making sure the events only fire once.我建议重写 function 以触发 mouseenter/mouseleave 事件,我认为这将帮助您确保事件只触发一次。 You may not even have to add/remove classes with this method..您甚至可能不必使用此方法添加/删除类..

$(".slide_box").live('mouseenter', function () {
    //animate up
})

$(".slide_box").live('mouseleave', function () {
    //animate down
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM