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

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

Linda Hamilton
Release: 2024-12-30 20:01:14
Original
293 people have browsed it

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

Programmatic Page Scrolling Disabling with jQuery

In JavaScript, disabling page scrolling is a common task. One approach involves utilizing jQuery's CSS manipulation capabilities. This method requires three steps:

  1. Disable page scrolling by setting body{ overflow: hidden; } in the CSS.
  2. Capture the current scroll position using scrollTop() and scrollLeft().
  3. Bind a scroll event handler to the body and reset the scroll position to the captured values.

An Alternative Approach

The provided approach effectively prevents page scrolling. However, there is a simpler and more comprehensive solution:

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

This code fully disables page scrolling by setting both the html and body tags to have hidden overflow and a fixed height of 100%. To restore scrolling, simply reset the CSS properties to their original values.

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

This solution has been tested and confirmed to work effectively in both Firefox and Chrome browsers.

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