Stop fadin/fadeout event jquery

落爺英雄遲暮 提交于 2019-12-11 16:24:48

问题


I have a dive that flashs once on click. If you press the button multiple times it will keep flashing until the click count has been executed. I would like to so then if the user was to click the button multiple times, it would stop flashing after the last click.

Example https://jsfiddle.net/va6njdry/

$('button').click(function () {
    $('div').fadeOut().fadeIn().fadeOut().fadeIn();
});

回答1:


Use stop to stop previous animations.

$('button').click(function () {
    $('div').stop(true, true).fadeOut().fadeIn().fadeOut().fadeIn();
    //       ^^^^^^^^^^^^^^^^
});

Demo: https://jsfiddle.net/tusharj/va6njdry/1/

Docs: http://api.jquery.com/stop/

Stop the currently-running animation on the matched elements.

Edit

Thanks to @AWolff

You can also use finish

$('button').click(function () {
    $('div').finish().fadeOut().fadeIn().fadeOut().fadeIn();
    //       ^^^^^^^^
});

Demo: https://jsfiddle.net/tusharj/va6njdry/4/

Docs: https://api.jquery.com/finish/

Stop the currently-running animation, remove all queued animations, and complete all animations for the matched elements.



来源:https://stackoverflow.com/questions/30842359/stop-fadin-fadeout-event-jquery

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