Home > Web Front-end > JS Tutorial > How Can I Temporarily Disable Scrolling During jQuery scrollTo Animations?

How Can I Temporarily Disable Scrolling During jQuery scrollTo Animations?

Mary-Kate Olsen
Release: 2024-12-20 01:57:09
Original
801 people have browsed it

How Can I Temporarily Disable Scrolling During jQuery scrollTo Animations?

Temporarily Disabling Scrolling During jQuery scrollTo Animation

When utilizing jQuery's scrollTo plugin, scrolling can disrupt the animation's smoothness. While you could resort to simply hiding the scrollbar, a better solution is to disable scrolling temporarily without affecting its visibility.

Solution

Instead of solely targeting the scroll event, the key is to also cancel related interaction events such as mouse, touch, and button scrolls. Here's a script you can use:

// Disable scrolling
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);
}

// Enable scrolling
function enableScroll() {
  window.removeEventListener('DOMMouseScroll', preventDefault, false);
  window.removeEventListener(wheelEvent, preventDefault, wheelOpt); 
  window.removeEventListener('touchmove', preventDefault, wheelOpt);
  window.removeEventListener('keydown', preventDefaultForScrollKeys, false);
}
Copy after login

Now, you can disable scrolling by calling disableScroll() and enable it again when desired with enableScroll().

The above is the detailed content of How Can I Temporarily Disable Scrolling During jQuery scrollTo Animations?. 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