Stop jQuery animations stacking

前端 未结 4 1997
误落风尘
误落风尘 2021-01-03 07:43

I have an Options box that hovers in the top right of the webpage. It\'s opacity is set to 10% so as not to be obstructive to users. When they hover over (mouseenter) it I

相关标签:
4条回答
  • 2021-01-03 08:20

    The problem with using stop before a fadein(), such as in this example:

    $("#button").hover(function () {
        $("#light").stop().fadeIn();
      },
      function () {
        $("#light").stop().fadeOut();
    });
    

    Is that the stop() will have the fade out stop at whatever opacity the element has faded to when the hover state is changed and will start at the same opacity when hovered over again.

    The result is that multiple rapid hovers will eventually fade out the element and won't fade in again.

    0 讨论(0)
  • 2021-01-03 08:36

    You can use the stop method there i think.

    Stop the currently-running animation on the matched elements.

    0 讨论(0)
  • 2021-01-03 08:37

    You can use the stop method which will stop animation and clear the animation queue.

    $("#dropdown").mouseenter(function() { 
        $(this).stop(true).fadeTo('fast',1);
        $("#options").stop(true).slideDown();
    });
    
    $("#dropdown").mouseleave(function() { 
        $(this).stop(true).fadeTo('fast',0.1);
        $("#options").stop(true).slideUp();
    });
    

    You might also look in to the hoverintent plugin

    0 讨论(0)
  • 2021-01-03 08:41

    Call stop to terminate any existing animations before starting a new one. You should also probably use hover instead of mouseenter/mouseleave.

    $("#dropdown").hover(function() {  
        $(this).stop().fadeTo('fast',1); 
        $("#options").stop().slideDown(); 
    }, function() {  
        $(this).stop().fadeTo('fast',0.1); 
        $("#options").stop().slideUp(); 
    });
    
    0 讨论(0)
提交回复
热议问题