How to programmatically disable page scrolling with jQuery

后端 未结 23 2331
滥情空心
滥情空心 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条回答
  •  萌比男神i
    2020-11-22 08:21

    I think the best and clean solution is:

    window.addEventListener('scroll',() => {
        var x = window.scrollX;
        var y = window.scrollY;
        window.scrollTo(x,y);
    });
    

    And with jQuery:

    $(window).on('scroll',() => {
        var x = window.scrollX;
        var y = window.scrollY;
        window.scrollTo(x,y)
    })
    

    Those event listener should block scrolling. Just remove them to re enable scrolling

提交回复
热议问题