javascript - Is this a good way to disable scrolling? -


i need disable scrolling of underlying content on page while modal showing. came across code disable page scrolling. (http://jsfiddle.net/77p2e/)

var $window = $(window), previousscrolltop = 0, scrolllock = false;  $window.scroll(function(event) {          if(scrolllock) {         $window.scrolltop(previousscrolltop);      }      previousscrolltop = $window.scrolltop(); });  $("#lock").click(function() {     scrolllock = !scrolllock; }); 

it seems work , cover possible scroll methods (wheel, middle mouse click, etc). there disadvantage using compared setting overflow: hidden or other method?

i prefer use pointer-events in combination overflow. disable interaction given dom element , partially limit interacting children.

.no-scroll {     overflow: hidden;     pointer-events: none; } 

if want disable scrolling after user has performed action, or other reason, can neatly apply class code conditionally.

because use jquery, can use addclass , removeclass functions toggle class on , off. can use toggleclass, depending on preference. prefer not use it, because addclass , removeclass makes me feel more in control.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -