Stop jquery function from multiple actions

I have a switch fade effect, but when you press the button several times, it disappears and exits more than once. (So ​​if I press the fade id button 20 times quickly, it will disappear and exit several times). How can I stop this and make it so that you can only switch the attenuation when the animation is done? I tried to get the current value and make an if statement, but that didn't work: * (

thanks

jQuery.fn.fadeToggle = function(speed, easing, callback) {
  return this.animate({opacity: 'toggle'}, speed, easing, callback);  
};



$(document).ready(function() {
  $('.open').click(function() {
    $('#fadeMe').next().fadeToggle('slow');
  });
});
+3
source share
2 answers

You need to check if the element is: animated, if so call .animate:

  <body>
    <button class="open">open</button><br>
    <div id="fadeMe">fade me!</div>
    <style>#fadeMe { 
    width:20em;
    height:10em;
    background:black;
    color:#fff;
    }</style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
    <script>
    jQuery.fn.fadeToggle = function(speed, easing, callback) {
      return this.animate({opacity: 'toggle'}, speed, easing, callback);  
    };



    $(document).ready(function() {
      $('.open').click(function() {
        var el = $('#fadeMe').next();
        if ( !el.is(':animated') ) {
            $(el).fadeToggle('slow');
        }
      });
    });
    </script>
    </body>

Demo: http://jsbin.com/irare

+6

.stop(). .

$('#fadeMe').next().stop().fadeToggle('slow');
+3

Source: https://habr.com/ru/post/1719344/


All Articles