Home > Web Front-end > CSS Tutorial > How Can I Efficiently Disable and Re-enable Page Scrolling with jQuery?

How Can I Efficiently Disable and Re-enable Page Scrolling with jQuery?

Barbara Streisand
Release: 2024-12-29 11:54:11
Original
484 people have browsed it

How Can I Efficiently Disable and Re-enable Page Scrolling with jQuery?

Disabling Page Scrolling with jQuery: An Enhanced Solution

In need of disabling the scrolling functionality of a webpage using jQuery? Here's a revised approach that offers a comprehensive solution:

Instead of relying on multiple event handlers, a more efficient method is to apply CSS styles directly to disable scrolling. The following code achieves this:

$('html, body').css({
    overflow: 'hidden',
    height: '100%'
});
Copy after login

By setting overflow to hidden and height to 100%, the page will be contained within the viewport, preventing any scrolling or scrolling-induced content shifting.

To restore scrolling functionality, simply reverse the styles:

$('html, body').css({
    overflow: 'auto',
    height: 'auto'
});
Copy after login

This revised approach effectively disables scrolling without the need for any complex event handling or previously captured scroll positions. It has been tested and works as expected on both Firefox and Chrome browsers.

The above is the detailed content of How Can I Efficiently Disable and Re-enable Page Scrolling with jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template