jQuery: Stop repeating animation on multiple rollovers?

吃可爱长大的小学妹 提交于 2019-12-17 14:54:14

问题


I'm having trouble stopping an animation...if i rollover a link to reveal a box 5 times and come off..the "Show" effect happens 5 times...as i come off it I only want it to show once....

Basically it seems to get stuck in a loop....

Any ideas how to stop the multiple instances?

jQuery(document).ready(function(){
    jQuery('.ttip').hover(
        function() {
            var offset = jQuery(this).offset();
            console.log(offset)
            var width = jQuery(this).outerWidth();
            var tooltipId = jQuery(this).attr("rel");
            jQuery('#tooltip-container').empty().load('tooltips.html' + tooltipId).fadeIn(500);
            jQuery('#tooltip-container').css({top:offset.top, left:offset.left + width + 10}).show();
        },
        function() {
          jQuery('#tooltip-container').fadeOut(500);
        });
});

回答1:


You should use stop(true).

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




回答2:


As Stefan says force the animation to jump to the end with a stop(true) like this example using your code:

jQuery(document).ready(function(){
jQuery('.ttip').hover(
    function() {
        var offset = jQuery(this).offset();
        console.log(offset)
        var width = jQuery(this).outerWidth();
        var tooltipId = jQuery(this).attr("rel");
        jQuery('#tooltip-container').empty().load('tooltips.html' + tooltipId).stop(true).fadeIn(500);
        jQuery('#tooltip-container').css({top:offset.top, left:offset.left + width + 10}).show();
    },
    function() {
      jQuery('#tooltip-container').stop(true).fadeOut(500);
    });
});


来源:https://stackoverflow.com/questions/9113568/jquery-stop-repeating-animation-on-multiple-rollovers

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