0

基本上我mouseleave在单击元素后取消绑定。但是我希望在单击另一个元素mouseleave让该元素的 -event 再次工作。我的代码可以运行,但感觉很笨重,因为我在代码中不断地重复动画。

mouseleave除了解除绑定和“重新绑定”之外,没有其他方法可以暂时抑制这种情况吗?有什么建议么 ?

这是我在 jsfiddle 上的示例

HTML

<div class="container">
 CONTAINER ONE
</div>

<div class="container">
 CONTAINER TWO
</div>

JS:

$(document).ready(function()
{
    //the default hover-event
    $('.container').hover(
        function(){
              $(this).stop().animate({'padding-bottom':'10px'},{queue:false,duration:160});
        },
        function() {
              $(this).stop().animate({'padding-bottom':'0px'},{queue:false,duration:160});
        }
    );



    $('.container').click(function(e) {
        e.preventDefault();

        // enables the mouseleave for all other `.container`s again.
        // also bring the old clicked element into unhovered position again
        $('.container')
                .bind('mouseleave',function() {
                    $(this).stop().animate({'padding-bottom':'0px'},{queue:false,duration:160});
                }).not(this).stop().animate({'padding-bottom':'0px'},{queue:false,duration:160});

        // supress the mouseleave for the clicked element
            $(this).unbind('mouseleave');
    })

});
4

1 回答 1

2

这可能是更好的方法(示例):

$(document).ready(function()
{
    var over = function(){
        $(this).stop().animate({'padding-bottom':'10px'},{queue:false,duration:160});
    };
    var out = function() {
        $(this).stop().animate({'padding-bottom':'0px'},{queue:false,duration:160});
    };
    //the default hover-event
    $('.container').hover(over, out);



    $('.container').click(function(e) {
        e.preventDefault();

        // enables the mouseleave for all other `.container`s again.
        // also bring the old clicked element into unhovered position again
        $('.container').bind('mouseleave', out).not(this).each(out);

        // supress the mouseleave for the clicked element
        $(this).unbind('mouseleave');
    })
});
于 2011-12-08T13:47:29.700 回答