Stop jQuery animations stacking

假如想象 提交于 2019-11-30 15:46:18

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(); 
});

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

You can use the stop method there i think.

Stop the currently-running animation on the matched elements.

raisinlove

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!