简体   繁体   中英

Javascript mouse wheel event stop

Is there a way to stop the mouse wheel event to stop triggering after after call? I am trying to switch between pages (one page at a time/ like one page per scroll but the scroll should happen at top 500ms) using this logic but the event keeps calling my method even if I remove the listener:

const listenToWheel = (event) => {
    console.log("SCROLL");
    removeScrollEventListener();
    console.log("DO STUFF");
    if (event.deltaY < 0) {
      getPreviousPage();
    } else if (event.deltaY > 0) {
      getNextPage();
    }
    setTimeout(() => {
      addScrollEventListener();
    }, 800);
  };


const addScrollEventListener = () => {
    window.addEventListener("wheel", listenToWheel);
  };

  const removeScrollEventListener = () => {
    window.removeEventListener("wheel", listenToWheel);
  };

Try writing your last 2 blocks like this:

const addScrollEventListener = () => {
    window.addEventListener("mousewheel", listenToWheel);
  };

  const removeScrollEventListener = () => {
    window.removeEventListener("mousewheel", listenToWheel);
  };

as some browsers prefer mousewheel event over wheel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM