【问题标题】:How to tell if element is currently actively scrolling如何判断元素当前是否正在滚动
【发布时间】:2021-11-08 19:47:39
【问题描述】:

我想在滚动元素时阻止某些事情发生。 滚动可能在使用 mouse wheel / mouse pad 时开始,但也可能在调用 scrollIntoView() 时发生。 有没有办法判断一个元素当前是否直接从该元素滚动?

【问题讨论】:

  • 所以您的意思不是页面本身,而是其中的子元素?

标签: javascript html reactjs typescript


【解决方案1】:

这里有一个函数可以帮助你检测去抖滚动,你可以在设置的计时器后开始调用任何函数。

let timeOut;
function debounce(delay) {
  console.log("It's rolling!");
  clearTimeout(timeOut);
  timeOut= setTimeout(function(){
    console.log("It's stopped!");
  }, delay);
}

document.querySelector("#scrollable").addEventListener('scroll',(function() {
  debounce(200);
}));
div {
  border: 1px solid black;
  width: 200px;
  height: 100px;
  overflow: scroll;
}
<div id="scrollable">In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.
<br><br>
'Whenever you feel like criticizing anyone,' he told me, just remember that all the people in this world haven't had the advantages that you've had.'</div>

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 2023-03-22
    • 2010-09-12
    相关资源
    最近更新 更多