Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

i prepared simple example for this question:

This is 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;
});
?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
262 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use on() and off() to turn the click function 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...