jquery禁用单击直到动画完全完成

时间:2022-11-21 20:06:35

i want to disable click function untill every code in it; initialized and completed.

我想禁用点击功能,直到其中的每个代码;初始化和完成。

i read these articles and tried what they said but cant make it happen: event.preventDefault() vs. return false + Best way to remove an event handler in jQuery? + jQuery - Disable Click until all chained animations are complete

我读了这些文章并尝试了他们所说的但是无法实现:event.preventDefault()vs return false +在jQuery中删除事件处理程序的最佳方法? + jQuery - 禁用单击直到所有链接的动画完成

i prepared simple example for this question:

我准备了这个问题的简单例子:

This is DEMO

这是DEMO

html

<div class="ele">1</div>
<div class="ele">2</div>
<div class="ele">3</div>

jQuery

$('.ele').click(function() {

    if ( !$('.ele').is(':animated') ) {//this works too but;
                         //is:animate duration returns = 2000 but need to be 4000
        $('.ele').stop().animate({'top':'0px'},2000);
        $(this).stop().animate({'top':'100px'},4000);
    } 
    return false;
});
​

2 个解决方案

#1


5  

Use on() and off() to turn the click function on/off :

使用on()和off()打开/关闭点击功能:

$('.ele').on('click', animateEle);

function animateEle(e) {
    $('.ele').stop().animate({'top':'0px'},2000);
    $(e.target).off('click').stop().animate({'top':'100px'},4000, function() {
        $(e.target).on('click', animateEle);
    });
}​

DEMONSTRATION

#2


0  

You can try this:

你可以试试这个:

var exec_a1 = true, exec_a2 = true;
$('.ele').click(function() {
    if (exec_a1 && exec_a2) {
        exec_a1 = false;
        exec_a2 = false;

        $('.ele').stop().animate({'top': '0px'}, 2000, function() {
            exec_a1 = true;
        });
        $(this).stop().animate({'top': '100px'}, 4000, function() {
            exec_a2 = true;
        });
    }
    return false;
});

#1


5  

Use on() and off() to turn the click function on/off :

使用on()和off()打开/关闭点击功能:

$('.ele').on('click', animateEle);

function animateEle(e) {
    $('.ele').stop().animate({'top':'0px'},2000);
    $(e.target).off('click').stop().animate({'top':'100px'},4000, function() {
        $(e.target).on('click', animateEle);
    });
}​

DEMONSTRATION

#2


0  

You can try this:

你可以试试这个:

var exec_a1 = true, exec_a2 = true;
$('.ele').click(function() {
    if (exec_a1 && exec_a2) {
        exec_a1 = false;
        exec_a2 = false;

        $('.ele').stop().animate({'top': '0px'}, 2000, function() {
            exec_a1 = true;
        });
        $(this).stop().animate({'top': '100px'}, 4000, function() {
            exec_a2 = true;
        });
    }
    return false;
});