Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable scrolling temporarily?

I'm using the scrollTo jQuery plugin and would like to know if it is somehow possible to temporarily disable scrolling on the window element through Javascript? The reason I'd like to disable scrolling is that when you scroll while scrollTo is animating, it gets really ugly ;)

Of course, I could do a $("body").css("overflow", "hidden"); and then put it back to auto when the animation stops, but it would be better if the scrollbar was still visible but inactive.

like image 537
Olivier Lalonde Avatar asked Jan 22 '11 19:01

Olivier Lalonde


People also ask

How do I disable scrolling on my website?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.

How do I disable scroll wheel?

Open the Registry Editor by clicking Start and typing regedit and hit enter. Navigate to HKEY_CURRENT_USER\Control Panel\Desktop and set WheelScrollChars and WheelScrollLines to 0. A reboot is necessary for this to take affect. This will disable scrolling, not the button itself.

How do I restrict scrolling in HTML?

body has a margin by default in different browsers, generally it is around 8px, so you can remove it by using margin: 0 . Your main css property should always come after your vendor prefixes such as -webkit- , -moz- , etc.


2 Answers

The scroll event cannot be canceled. But you can do it by canceling these interaction events:
Mouse & Touch scroll and Buttons associated with scrolling.

[Working demo]

// left: 37, up: 38, right: 39, down: 40, // spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36 var keys = {37: 1, 38: 1, 39: 1, 40: 1};  function preventDefault(e) {   e.preventDefault(); }  function preventDefaultForScrollKeys(e) {   if (keys[e.keyCode]) {     preventDefault(e);     return false;   } }  // modern Chrome requires { passive: false } when adding event var supportsPassive = false; try {   window.addEventListener("test", null, Object.defineProperty({}, 'passive', {     get: function () { supportsPassive = true; }    })); } catch(e) {}  var wheelOpt = supportsPassive ? { passive: false } : false; var wheelEvent = 'onwheel' in document.createElement('div') ? 'wheel' : 'mousewheel';  // call this to Disable function disableScroll() {   window.addEventListener('DOMMouseScroll', preventDefault, false); // older FF   window.addEventListener(wheelEvent, preventDefault, wheelOpt); // modern desktop   window.addEventListener('touchmove', preventDefault, wheelOpt); // mobile   window.addEventListener('keydown', preventDefaultForScrollKeys, false); }  // call this to Enable function enableScroll() {   window.removeEventListener('DOMMouseScroll', preventDefault, false);   window.removeEventListener(wheelEvent, preventDefault, wheelOpt);    window.removeEventListener('touchmove', preventDefault, wheelOpt);   window.removeEventListener('keydown', preventDefaultForScrollKeys, false); } 

UPDATE: fixed Chrome desktop and modern mobile browsers with passive listeners

like image 119
25 revs, 4 users 83% Avatar answered Sep 29 '22 03:09

25 revs, 4 users 83%


Do it simply by adding a class to the body:

.stop-scrolling {   height: 100%;   overflow: hidden; } 

Add the class then remove when you want to re-enable scrolling, tested in IE, FF, Safari and Chrome.

$('body').addClass('stop-scrolling') 

For mobile devices, you'll need to handle the touchmove event:

$('body').bind('touchmove', function(e){e.preventDefault()}) 

And unbind to re-enable scrolling. Tested in iOS6 and Android 2.3.3

$('body').unbind('touchmove') 
like image 41
hallodom Avatar answered Sep 29 '22 02:09

hallodom