Stop ScrollTop function when user scrolls jquery

霸气de小男生 提交于 2021-02-07 08:49:16

问题


How can i stop the scrollTop function when the user scrolls himself? If i scroll now while the scrollTop function the whole scrolling glitches out.

        $(document).ready(function (){
            $(".header-arrow-down").click(function (){
                $('html, body').animate({
                    scrollTop: $(".page-1").offset().top
                }, 1000);
            });
            $("body").scroll(function() {
                alert("scrolling");
            });
        });

I tried this so far but the alert scrolling isnt showing up for me and i can't find a solution on google so im asking it here.

I hope someone knows how to fix this thanks!


回答1:


you need to unbind or stop the animation when user scrolls, sth like

$("html, body").bind("scroll mousedown DOMMouseScroll mousewheel keyup", function(){
    $('html, body').stop();
});



回答2:


Hey I just wrote that code today for a project I'm working on:

$('body,html').bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup', function(e){
    if (e.which > 0 || e.type == "mousedown" || e.type == "mousewheel"){
    $("html,body").stop();
    }
});


来源:https://stackoverflow.com/questions/26379182/stop-scrolltop-function-when-user-scrolls-jquery

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