How do you cancel a jQuery fadeOut() once it has started?

后端 未结 3 1383
迷失自我
迷失自我 2020-12-18 17:59

I have a basic div element to represent a message that I show for a few seconds and then fade it out using

$(\'#message\').fadeOut(5000);

I

相关标签:
3条回答
  • 2020-12-18 18:14

    Check out the stop function

    http://docs.jquery.com/Effects/stop#clearQueuegotoEnd

    0 讨论(0)
  • 2020-12-18 18:20

    Also, you can test if an element is in the middle of an animation using the :animated selector:

    $('#message').mouseover(
        function () {
          if($(this).is(':animated')) {
             $(this).stop().animate({opacity:'100'});
          }
        }
    );
    
    0 讨论(0)
  • 2020-12-18 18:28

    In my case stop() merely didn't work at least in Firefox, after searching I figured out that It should be stop(true, true):

    $('#message').mouseover(
        function () {
             $(this).stop(true, true).fadeOut();
        }
    );
    

    stop(): Stops the currently-running animation on the matched elements.

    or even you can use finish() instead:

    $('#message').mouseover(
        function () {
             $(this).finish().fadeOut();
        }
    );
    

    but there is a side effect about finish(), it stops all other running animations too.

    finish(): Stops the currently-running animation, remove all queued animations, and complete all animations for the matched elements.

    Read more here.

    0 讨论(0)
提交回复
热议问题