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
Post a Comment