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

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

Mary-Kate Olsen
Release: 2024-12-24 14:10:14
Original
781 people have browsed it

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

How to Disable Page Scrolling with jQuery

Disabling page scrolling can be achieved through a variety of methods, including setting overflow: hidden on the body element. However, there are more effective and precise approaches using jQuery.

One such method involves binding to the body's scroll event and resetting the scrollTop/scrollLeft to a captured value. While this technique works in most scenarios, it may not handle edge cases where the page content is larger than the viewport.

For a more robust solution, consider using the following jQuery code:

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

This code completely disables scrolling on both the HTML and body elements by setting overflow: hidden and ensuring a full-height page.

To restore scrolling, simply reverse the changes:

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

This approach is more reliable and consistent across browsers, ensuring that the page remains unscrollable even when the content is larger than the viewport.

The above is the detailed content of How Can I 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