JavaScript: detect page is scrolling on scroll-behavior: smooth and deprecate scroll event in this case

huangapple go评论65阅读模式
英文:

JavaScript: detect page is scrolling on scroll-behavior: smooth and deprecate scroll event in this case

问题

function scrollHandler() {
  if (!document.body.classList.contains('smooth-scrolling')) {
    console.log("scroll");
  }
}

window.addEventListener("scroll", throttledScrollHandler);
window.addEventListener("click", (e) => {
  if (e.target.closest("a")) {
    document.body.classList.add('smooth-scrolling');
    setTimeout(() => {
      document.body.classList.remove('smooth-scrolling');
    }, 1000);
    console.log("clicked");
  }
});
英文:

I have a scroll event and I have links in HTML which smoothly scroll page content via scroll-behavior: smooth. And I need my scroll event execute ONLY if there is no scroll by link click.

Here is a small example:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function scrollHandler() {
  console.log(&quot;scroll&quot;);
}

const throttledScrollHandler = throttle(scrollHandler, 200);

window.addEventListener(&quot;scroll&quot;, throttledScrollHandler);
window.addEventListener(&quot;click&quot;, (e) =&gt; {
  if (e.target.closest(&quot;a&quot;)) {
    // I can kill this event listener onclick, but when could I disable it?
    //window.removeEventListener(&#39;scroll&#39;, scrollHandler)
    console.log(&quot;clicked&quot;);
  }
});

function throttle(callback, delay) {
  let timer = null;

  return function throttledFunc(...args) {
    if (timer) {
      return;
    }

    timer = setTimeout(() =&gt; {
      callback(...args);

      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
    }, delay);
  };
}

<!-- language: lang-css -->

html {
  scroll-behavior: smooth;
}

body {
  font-family: Arial;
}

section {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 150vh;
  font-size: 40px;
  color: #fff;
}

section:nth-of-type(1) {
  background-color: black;
}

section:nth-of-type(2) {
  background-color: grey;
}

section:nth-of-type(3) {
  background-color: black;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Parcel Sandbox&lt;/title&gt;
    &lt;meta charset=&quot;UTF-8&quot; /&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;./src/styles.css&quot; /&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;nav&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;#section1&quot;&gt;Section 1&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#section2&quot;&gt;Section 2&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#section3&quot;&gt;Section 3&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/nav&gt;

    &lt;section id=&quot;section1&quot;&gt;Section 1&lt;/section&gt;
    &lt;section id=&quot;section2&quot;&gt;Section 2&lt;/section&gt;
    &lt;section id=&quot;section3&quot;&gt;Section 3&lt;/section&gt;
  &lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

I need to do something to prevent here scroll mark in console if I click a link, but stay if I simply scroll.

Here is a codesandbox with this example

答案1

得分: 1

你可能需要使用wheel事件而不是scroll事件。以下是可以对你的JavaScript进行的更改以获得所需的效果。

function scrollHandler() {
  console.log("scroll");
}

const throttledScrollHandler = throttle(scrollHandler, 200);

window.addEventListener("wheel", throttledScrollHandler);

window.addEventListener("click", (e) => {
  if (e.target.closest("a")) {

    console.log("clicked")

  }
});

function throttle(callback, delay) {
  let timer = null;

  return function throttledFunc(...args) {
    if (timer) {
      return;
    }

    timer = setTimeout(() => {
      callback(...args);

      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
    }, delay);
  };
}
英文:

You might need to use the wheel event instead of the scroll event. Here are the changes you can make to your javascript to get the desired effect.

function scrollHandler() {
  console.log(&quot;scroll&quot;);
}

const throttledScrollHandler = throttle(scrollHandler, 200);

window.addEventListener(&quot;wheel&quot;, throttledScrollHandler ); 

window.addEventListener(&quot;click&quot;, (e) =&gt; {
  if (e.target.closest(&quot;a&quot;)) {
  
    console.log(&quot;clicked&quot;)
   
  }
});



function throttle(callback, delay) {
  let timer = null;

  return function throttledFunc(...args) {
    if (timer) {
      return;
    }

    timer = setTimeout(() =&gt; {
      callback(...args);

      if (timer) {
        clearTimeout(timer);
        timer = null;
      }
    }, delay);
  };
}
``

</details>



huangapple
  • 本文由 发表于 2023年3月31日 19:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定