How to programmatically disable page scrolling with jQuery

后端 未结 23 2069
滥情空心
滥情空心 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
    慢半拍i (楼主)
    2020-11-22 08:36

    One liner to disable scrolling including middle mouse button.

    $(document).scroll(function () { $(document).scrollTop(0); });
    

    edit: There's no need for jQuery anyway, below same as above in vanilla JS(that means no frameworks, just JavaScript):

    document.addEventListener('scroll', function () { this.documentElement.scrollTop = 0; this.body.scrollTop = 0; })
    

    this.documentElement.scrollTop - standard

    this.body.scrollTop - IE compatibility

提交回复
热议问题