1

I use window.addEventListener to handle scrolling events.

componentDidMount() {
  window.addEventListener("scroll", this.handleScroll)
}

I would like to disable that event handler when scrolling is triggered programatically by window.scroll vs user.

sunknudsen
  • 6,356
  • 3
  • 39
  • 76

1 Answers1

0

Here's a little useful trick...

if(event.isTrusted){

 // human gesture

} else {

// automated

}
  • Thanks! Promising but given `window.scroll` is called by the user, `event.isTrusted` always returns `true`. Perhaps I am missing something? – sunknudsen Aug 15 '20 at 13:22
  • This isTrusted gives true if some one scroll it by schanging the scrollTop. So this has false positive: https://jsfiddle.net/asutosh/rf9zakym/11/ – Asutosh Aug 15 '20 at 13:23
  • See https://stackoverflow.com/questions/36656858/why-does-calling-window-scroll-give-a-trusted-event – sunknudsen Aug 15 '20 at 13:25
  • Something like window.scrollTo(100,0) is not a human gesture. – user4723924 Aug 15 '20 at 13:25
  • Agreed, but it is triggered by a human so I believe it would register `isTrusted` as `true`. – sunknudsen Aug 15 '20 at 13:28
  • Well then you'll have to set up a global flag and set it as required and where required. – user4723924 Aug 15 '20 at 13:32
  • Sadly, looks like few elegant options exist to distinguish between these events. – sunknudsen Aug 15 '20 at 13:37
  • Btw, I see you are new to StackOverflow. Not all people ruthlessly downvote answers (I know it can be demotivating). Thanks for trying to help and welcome! – sunknudsen Aug 15 '20 at 13:45