Skip to content
Fix Code Error

How to disable scrolling temporarily?

March 13, 2021 by Code Error
Posted By: Anonymous

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.

Solution

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

Answered By: Anonymous

Related Articles

  • Use of Jquery on scroll event
  • Animating Elements in One by One
  • CSS customized scroll bar in div
  • Issue with iron-ajax request
  • easiest way to extract Oracle form xml format data
  • Is it possible to apply CSS to half of a character?
  • Why are CSS keyframe animations broken in Vue…
  • How do i arrange images inside a div?
  • Adobe XD to responsive html
  • jQuery Mobile: document ready vs. page events
  • How to add 'load more' functionality to items on a…
  • How to Fix hover not working after JS script…
  • Fix top buttons on scroll of list below
  • Do not close the mode in the mouse up event
  • JavaScript Code not running on a Live Sever nor Local Host
  • Polymer Neon-Animation: How to use…
  • How to use html template with vue.js
  • Reset/remove CSS styles for element only
  • After a little scroll, the sticky navbar just is not…
  • Android Layout Animations from bottom to top and top…
  • Sort table rows In Bootstrap
  • How to parse an RSS feed using JavaScript?
  • HTML table with 100% width, with vertical scroll…
  • The Aurelia way of adding a horizontal top scrollbar…
  • How to hide header elements if…
  • Javascript - Track mouse position
  • How do you create a custom neon animation in Dart
  • Dynamically update values of a chartjs chart
  • Having trouble with my nav bar/header, It used to…
  • How to change the active tab in a responsive…
  • Active tab issue on page load HTML
  • How do I import a pre-existing Java project into…
  • How to work out how to include jquery plugin with…
  • Animate a UIBezierPath hexagon like UIActivityIndicatorview
  • Eclipse will not start and I haven't changed anything
  • Specifying java version in maven - differences…
  • Polymer - Ripple Animation Skews Hero Animation
  • Aurelia pass configuration to plugin or feature
  • Making a div vertically scrollable using CSS
  • How does PHP 'foreach' actually work?
  • PostgreSQL Crosstab Query
  • polymer paper-scroll-header-panel doesn't display
  • Adding animation to QPushbutton enterEvent and exitEvent
  • Aurelia bundling issue with virtual directory
  • CSS overflow-x: visible; and overflow-y: hidden;…
  • How to change the scrollbar color using css
  • Vertical scroll inside paper-header-panel behaving…
  • Downloading jQuery UI CSS from Google's CDN
  • paper-dialog stretching off screen
  • Javascript Drag and drop for touch devices
  • Aurelia/Karma/Istanbul Cannot read property 'skip'…
  • does not work with ?
  • Show div on scrollDown after 800px
  • Missing visible-** and hidden-** in Bootstrap v4
  • What does this symbol mean in JavaScript?
  • Why does the data-binding polyfill in Polymer not…
  • Prevent body scrolling but allow overlay scrolling
  • Jquery fadeToggle Trouble
  • How can I make setInterval also work when a tab is…
  • How do I keep only the first map and when the game…
  • CSS3 scrollbar styling on a div
  • What is causing this broken animation/transition in…
  • jQuery trouble combining 2 functions
  • Basic Ember Template Not Rendering in Rails
  • "Thinking in AngularJS" if I have a jQuery background?
  • Docker compose fails to start a service with an…
  • Centering in CSS Grid
  • Scrollable Menu with Bootstrap - Menu expanding its…
  • How to programmatically disable page scrolling with jQuery
  • How do I get neon-animation to work in Polymer Dart 1.0
  • What methods of ‘clearfix’ can I use?
  • Hide scroll bar, but while still being able to scroll
  • Second animation with @keywords does not apply to div
  • Onclick, the button moves down, why can that due to?
  • jQuery `.is(":visible")` not working in Chrome
  • javascript .replace and .trim not working in vuejs
  • Polymer Nested Core-Scaffold and Multiple Vertical…
  • Could not calculate build plan: Plugin…
  • Smart way to truncate long strings
  • CSS3 Rotate Animation
  • jQuery.on() Delegation: Slightly complex selector…
  • Polymer neon-animations example not animating
  • C++ OpenGL stb_image.h errors
  • How to remove element from array in forEach loop?
  • Only show HTML body after a CSS animation is…
  • CSS grid wrapping
  • Show/hide 'div' using JavaScript
  • Make Iframe to fit 100% of container's remaining height
  • Aurelia import library after library
  • Difference between variable declaration syntaxes in…
  • JavaScript plugin initialization in Polymer Shadow DOM
  • Use mobile scrollbar in Polymer
  • HTML 5 Favicon - Support?
  • Navbar not filling width of page when reduced to mobile view
  • Javascript and SVG - move element with offset
  • bootstrap 4 responsive utilities visible / hidden xs…
  • Full-screen iframe with a height of 100%
  • How to handle scroll position on hashchange in…
  • Scroll inside a div conten overflow text
  • Aurelia UX showcase app fails to load

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to search for a string in text files?

Next Post:

string to string array conversion in java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error