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 に答える