Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping .click() listener until fadeIn() is finished in jQuery

$('#div1_button').click(function() {

    $('#div0').fadeOut(function(){          
        $('#div1').fadeIn();    
    });

});

When a user clicks div1_button the previously selected div0 fades out and div1 fades in. If the user goes click crazy and clicks div2 before div1 is finished fading in then div2 begins to fade in and eventually div1 fades out, but they stack on top of each other until div1 is finished fading in then fades out. How can I stop the .click() event until the clicked div is finished fading in.

like image 953
bmck Avatar asked Sep 24 '10 14:09

bmck


4 Answers

Something like

var div1_bclick_inprogress = false;
$('#div1_button').click(function() {
    if (!div1_bclick_inprogress) {
        div1_bclick_inprogress = true;
        $('#div0').fadeOut(function(){          
            $('#div1').fadeIn(function(){
                 div1_bclick_inprogress = false;
            });    
        });
    }

});

but you may have to experiment a bit with the details

like image 184
einarmagnus Avatar answered Nov 07 '22 20:11

einarmagnus


USE :animated .. http://api.jquery.com/animated-selector/

Here: an example

$("#div1_button").click(function() {
    if (!$(this).parent().children().is(':animated')) {
            $('#div0').fadeOut(function(){          
                $('#div1').fadeIn();    
          });
    }
    return false;
});
like image 29
Stewie Avatar answered Nov 07 '22 18:11

Stewie


You can stop animations by using the jQuery .stop() function. http://api.jquery.com/stop/

$('#div1_button').click(function() {

    $('#div0').stop(true, true).fadeOut(function(){          
        $('#div1').stop(true, true).fadeIn();    
    });

});

While this is not exactly what you requested, it's definitely what I would've done.

like image 2
Niv Avatar answered Nov 07 '22 18:11

Niv


don't you think that is better to stop the fadeIn/fadeOut and change the direction as the user requested?

in this case:

$('#div1_button').click(function() {
    var state = $(this).data("state");
    $(this).data(state, !state);

    var d0 = $("#div0").stop(),
        d1 = $("#div1").stop();

    if (state) {
      d0.fadeOut(function() {          
        d1.fadeIn();    
      });
    } else {
      d0.fadeIn(function() {
        d1.fadeOut();
      });
    }
});

or something like this

like image 2
KARASZI István Avatar answered Nov 07 '22 20:11

KARASZI István