Disable all scrolling on webpage

前端 未结 1 879
余生分开走
余生分开走 2021-01-19 03:20

I would like to know if it is possible to disable all scrolling on a webpage.

I am currently using

html, body { overflow:hidden; }

相关标签:
1条回答
  • 2021-01-19 03:44

    I have had this exact same issue, i fixed it with the following;

    var disableScroll = false;
    var scrollPos = 0;
    function stopScroll() {
        disableScroll = true;
        scrollPos = $(window).scrollTop();
    }
    function enableScroll() {
        disableScroll = false;
    }
    $(function(){
        $(window).bind('scroll', function(){
             if(disableScroll) $(window).scrollTop(scrollPos);
        });
        $(window).bind('touchmove', function(){
             $(window).trigger('scroll');
        });
    });
    

    the touch move is bound to the window as the window scroll event is not fired until touch move is completed, so this allows a much smoother experience on iOS!

    This isn't a perfect solution as you can 'throw' the page, but it will return to desired position when the throw has complete (as the window scroll event will then be fired). This is because iOS browsers strip out a lot of events for performance. also setTimeout and setInterval functions do not fire whilst the page is being thrown, having a loop isn't an option either!

    see here http://jsfiddle.net/8T26k/

    0 讨论(0)
提交回复
热议问题