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
Mary-Kate OlsenOriginal
2024-12-20 01:57:09802browse

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);
}

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!

Statement:
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