Call Scroll only when user scrolls, not when animate()

后端 未结 3 484
清酒与你
清酒与你 2020-12-03 16:32

I have a few links across the page with the purpose of \"going to the top\", accomplished by scrolling the page to the top with a nice animation. I\'ve noticed that sometime

3条回答
  •  春和景丽
    2020-12-03 16:56

    Figured it out! After looking around on the Internet I found something called Document.addEventListener for Mozilla and document.onmousewheel for IE and Opera that will catch scrolling events.

    $('#gototop').click(function() {
        $('body').animate({scrollTop:0},3000);
    
        if(window.addEventListener) document.addEventListener('DOMMouseScroll', stopScroll, false);
        document.onmousewheel = stopScroll;
    
        function stopScroll() {$('body').stop()};
    
        return false;
    })
    

提交回复
热议问题