2

I have a small website that starts to scroll to show user the entire content after some short time of inactivity.

I have one event listener that listens if user is active and if not it starts scrolling automatically. The problem is that this automatic scroll makes the website think that it's scroll by the user, which is not.

Is there a way how to tell difference if the scroll is done by user or by JavaScript?

// this line in an infinite loop using is causing the page to scroll
window.scrollTo(0, 200)
// this is detecting the scrolling
window.onscroll = (event) => {
   // maybe get the information somehow from the event??
   if (event.scrolledBy == 'user'){
       activity = true
   }
}

can't I duplicate the function and set some sort of id of it? Let's say

window.scrollToAutomat = window.scrollTo

and then do some magic?

Jansindl3r
  • 360
  • 3
  • 18
  • 2
    Can't you set a sort of state somewhere every time the automatic scrolling gets fired? This same state can be updated whenever this automatic scrolling is done. It should be easier to identify this scrolling than the one fired by the user. – emerson.marini Oct 30 '20 at 15:06
  • 1
    think about what might the user do to make the scroll, identify the user with mouse events, put a boolean in the mousemovement eventHandler and if true reset it after the scroll – EugenSunic Oct 30 '20 at 15:06
  • Unfortunately `isTrusted` is `true` when you use `scrollTo`. Eugen's idea might be the best – CertainPerformance Oct 30 '20 at 15:08
  • 1
    Does this answer your question? [Detect whether scroll event was created by user](https://stackoverflow.com/questions/7035896/detect-whether-scroll-event-was-created-by-user) – Preda Bogdan Oct 30 '20 at 15:09
  • @melancia it sort of fires sequence of commands to scroll to given position so that's probably not possible. You could also wait until it's done, but it can also get interrupted by the user. And never achieve the destination it was going to – Jansindl3r Oct 30 '20 at 15:10
  • 2
    From the suggested question (@PredaBogdan), that's the answer that works around what I mentioned: https://stackoverflow.com/a/47553150/2191976 – emerson.marini Oct 30 '20 at 15:11
  • @EugenSunic, listening for mouse events would not be sufficient as the user might be scrolling the page in many different ways (e.g. pgUp/pgDown keys, up/down keys, tab key jumping from an element to the following one, mouse wheel, click & drag on the scrolling bar, etc...). Listening for all potential events would be a nightmare. Even more so if you have to take into consideration touch events too. – secan Oct 30 '20 at 15:14
  • @melancia thanks, that looks promising. I will check it. It was matter of naming the problem right and search the solution – Jansindl3r Oct 30 '20 at 15:14
  • @secan that s true you d have to cover those "edge" cases, it was just a suggestion on to initialy approach the problem – EugenSunic Oct 30 '20 at 15:15
  • I am a bit more Python person, so I would assume that cloning the function and changing its insides would work. "Ok scrollTo clone, now you are clone behave like this and that", is something like that not a possible solution? css `scroll-behavior: smooth;` makes it impossible to use @melancia solution, because then it doesn't trigger one scroll but multiple scroll events – Jansindl3r Oct 30 '20 at 15:45

0 Answers0