How to programmatically disable page scrolling with jQuery

后端 未结 23 2324
滥情空心
滥情空心 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:35

    You can cover-up the window with a scrollable div for preventing scrolling of the content on a page. And, by hiding and showing, you can lock/unlock your scroll.

    Do something like this:

    #scrollLock {
        width: 100%;
        height: 100%;
        position: fixed;
        overflow: scroll;
        opacity: 0;
        display:none
    }
    
    #scrollLock > div {
        height: 99999px;
    }
    
    function scrollLock(){
        $('#scrollLock').scrollTop('10000').show();
    }
    
    function scrollUnlock(){
        $('#scrollLock').hide();
    }
    

提交回复
热议问题