How to programmatically disable page scrolling with jQuery

后端 未结 23 2080
滥情空心
滥情空心 2020-11-22 08:09

Using jQuery, I would like to disable scrolling of the body:

My idea is to:

  1. Set body{ overflow: hidden;}
  2. Capture the current
相关标签:
23条回答
  • 2020-11-22 08:40

    This is what I ended up doing:

    CoffeeScript:

        $("input").focus ->
            $("html, body").css "overflow-y","hidden"
            $(document).on "scroll.stopped touchmove.stopped mousewheel.stopped", (event) ->
                event.preventDefault()
    
        $("input").blur ->
            $("html, body").css "overflow-y","auto"
            $(document).off "scroll.stopped touchmove.stopped mousewheel.stopped"
    

    Javascript:

    $("input").focus(function() {
     $("html, body").css("overflow-y", "hidden");
     $(document).on("scroll.stopped touchmove.stopped mousewheel.stopped", function(event) {
       return event.preventDefault();
     });
    });
    
    $("input").blur(function() {
     $("html, body").css("overflow-y", "auto");
     $(document).off("scroll.stopped touchmove.stopped mousewheel.stopped");
    });
    
    0 讨论(0)
  • 2020-11-22 08:41

    try this

    $('#element').on('scroll touchmove mousewheel', function(e){
      e.preventDefault();
      e.stopPropagation();
      return false;
    })
    
    0 讨论(0)
  • 2020-11-22 08:43

    I've written a jQuery plugin to handle this: $.disablescroll.

    It prevents scrolling from mousewheel, touchmove, and keypress events, such as Page Down.

    There's a demo here.

    Usage:

    $(window).disablescroll();
    
    // To re-enable scrolling:
    $(window).disablescroll("undo");
    
    0 讨论(0)
  • 2020-11-22 08:44

    I just provide a little tuning to the solution by tfe. In particular, I added some additional control to ensure that there is no shifting of the page content (aka page shift) when the scrollbar is set to hidden.

    Two Javascript functions lockScroll() and unlockScroll() can be defined, respectively, to lock and unlock the page scroll.

    function lockScroll(){
        $html = $('html'); 
        $body = $('body'); 
        var initWidth = $body.outerWidth();
        var initHeight = $body.outerHeight();
    
        var scrollPosition = [
            self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
            self.pageYOffset || document.documentElement.scrollTop  || document.body.scrollTop
        ];
        $html.data('scroll-position', scrollPosition);
        $html.data('previous-overflow', $html.css('overflow'));
        $html.css('overflow', 'hidden');
        window.scrollTo(scrollPosition[0], scrollPosition[1]);   
    
        var marginR = $body.outerWidth()-initWidth;
        var marginB = $body.outerHeight()-initHeight; 
        $body.css({'margin-right': marginR,'margin-bottom': marginB});
    } 
    
    function unlockScroll(){
        $html = $('html');
        $body = $('body');
        $html.css('overflow', $html.data('previous-overflow'));
        var scrollPosition = $html.data('scroll-position');
        window.scrollTo(scrollPosition[0], scrollPosition[1]);    
    
        $body.css({'margin-right': 0, 'margin-bottom': 0});
    }
    

    where I assumed that the <body> has no initial margin.

    Notice that, while the above solution works in most of the practical cases, it is not definitive since it needs some further customization for pages that include, for instance, an header with position:fixed. Let's go into this special case with an example. Suppose to have

    <body>
    <div id="header">My fixedheader</div>
    <!--- OTHER CONTENT -->
    </body>
    

    with

    #header{position:fixed; padding:0; margin:0; width:100%}
    

    Then, one should add the following in functions lockScroll() and unlockScroll():

    function lockScroll(){
        //Omissis   
    
    
        $('#header').css('margin-right', marginR);
    } 
    
    function unlockScroll(){
        //Omissis   
    
        $('#header').css('margin-right', 0);
    }
    

    Finally, take care of some possible initial value for the margins or paddings.

    0 讨论(0)
  • 2020-11-22 08:47

    Not sure if anybody has tried out my solution. This one works on the whole body/html but no doubt it can be applied to any element that fires a scroll event.

    Just set and unset scrollLock as you need.

    var scrollLock = false;
    var scrollMem = {left: 0, top: 0};
    
    $(window).scroll(function(){
        if (scrollLock) {
            window.scrollTo(scrollMem.left, scrollMem.top);
        } else {
            scrollMem = {
                left: self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
                top: self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
            };
        }
    });
    

    Here's the example JSFiddle

    Hope this one helps somebody.

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