How to use stop() properly in jQuery animation with hover event?

折月煮酒 提交于 2020-01-01 11:46:06

问题


I use the method below to make some animation. But when I move my mouse in and out really fast and stop it inside the div , the fadeIn() doesn't work and the div keeps transparent.

$(".grids").hover(function() {
    $('.gridscontrol').stop().fadeIn(200);
}, function() {
    $('.gridscontrol').stop().fadeOut(200);
});

回答1:


.stop() without parameters simply stops the animation, still leaving it in queue. In this case you want .stop(true) to clear the animation queue as well.

$(".grids").hover(function() {
  $('.gridscontrol').stop(true).fadeTo(200, 1);
}, function() {
  $('.gridscontrol').stop(true).fadeTo(200, 0);
});

Also note the use of .fadeTo() since .fadeIn() and .fadeOut() shortcuts have some undesirable behavior here. You can see a working example here.




回答2:


.stop( [clearQueue ] [, jumpToEnd ] )

Set both clearQueue and jumpToEnd to true.

$(".grids").hover(function() {
    $('.gridscontrol').stop(true, true).fadeIn(200);
}, function() {
    $('.gridscontrol').stop(true, true).fadeOut(200);
});


来源:https://stackoverflow.com/questions/14616818/how-to-use-stop-properly-in-jquery-animation-with-hover-event

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