简体   繁体   中英

jQuery animation triggers callback twice when run

I'm animating a scroll to effect using jQuery, after the animation ends, I trigger an effect, for some reason the effect is triggered twice, how can I prevent it from happening?

$('.something').on('click', function() {
            $('html, body').animate({
                scrollTop: $('footer').offset().top
            }, {
                queue: false,
                duration: 1500,
                complete: function() {
                    $('.foo').toggleClass('active');
                    $('.bar').slideToggle();    
                }
            });                
            return false;
        });

The slideToggle effect seems to be triggered twice. I have it animate on html & body because animate from 'body' doesn't work in IE8.

Since you're triggering two animations (on html and body) the complete callback should be called twice.

You might want to on trigger the animation on html only if it's required

var animateOn = isIE8 ? 'html' : 'body';
$(animateOn).animate();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM