【问题标题】:How to bind/unbind click to prevent animate's queue chaos?如何绑定/取消绑定点击以防止动画的队列混乱?
【发布时间】:2011-12-01 13:55:38
【问题描述】:

这个不行。动画队列最终陷入混乱。我做错了什么?请帮忙。

$button.bind('click',showHideFavourites);

function showHideFavourites() {
    if ($favContent.css('opacity') == 1) { 
        hideFavourites(); 
    } else { 
        showFavourites(); 
    }
}

function hideFavourites() { 
    $button.unbind('click');

    $favContent.animate({'opacity':'0'},250,function() { 
        $favourites.animate({'height':0},{queue:false,duration:250,easing:'easeInOutQuad',complete:function() {
            $button.text(showFav);

            $button.bind('click',showFavourites);
        }});
    });
}

function showFavourites() {
    $button.unbind('click');

    $favourites.animate({'height':fl_openHeight},{queue:false,duration:250,easing:'easeInOutQuad',complete:function() {
        $favContent.animate({'opacity':'1'},250);
        $button.text(hideFav);

        $button.bind('click',hideFavourites);   
        });
    }});
}

【问题讨论】:

    标签: jquery queue jquery-animate bind unbind


    【解决方案1】:
    $button.on('click', function() {
        $favContent.css('opacity') == 1) ? hideFavourites() : showFavourites(); 
    });
    
    function hideFavourites() { 
        $favContent.stop(true, true).animate({'opacity':'0'},250,function() { 
            $favourites.stop(true, true).animate({'height':0}, 250, 'easeInOutQuad', function() {
                $button.text(showFav);
            });
        });
    }
    
    function showFavourites() {
        $favourites.stop(true, true).animate({'height':fl_openHeight}, 250 'easeInOutQuad' function() {
            $button.text(hideFav);
            $favContent.stop(true, true).animate({'opacity':'1'},250);
         });
    }
    

    如果您尝试同时为高度和不透明度设置动画,您也应该放弃链接功能,我没有,因为我不确定 noqeue 的用途?

    【讨论】:

    • 谢谢。 .on 是怎么回事,我从来没有听说过。首先我认为应该是.one,但在这种情况下这是没有意义的。所以我用.on测试了它,它确实做到了,但是当我快速点击按钮两三下时,队列问题仍然存在。
    • .on() 是新的,您仍然可以使用 .click(),但我使用 on 因为它取代了 bind、live、click 等。这使得不必选择一个更容易。动画队列建立的问题可能可以通过 .stop(); 解决。有关更多信息,请参阅 jQuery 站点,但 stop() 会停止动画,而 stop(true) 会清除队列,而 stop(true, true) 会清除队列并将动画直接带到结尾。我在示例中添加了一些停靠点来展示它是如何完成的,但您通常需要进行一些试验才能使其适合您的特定用途。
    猜你喜欢
    • 2013-12-28
    • 2014-09-24
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 2011-04-24
    • 2010-12-20
    相关资源
    最近更新 更多