It is known that scroll
event could be fired up by using mouse-wheel, clicking on scrollbar arrows or dynamically with window.scrollTo(left, top)
function.
Is it possible to determine what caused the scroll
event to fire up? Whether it was user intervention or JS code?
It is known that scroll
event could be fired up by using mouse-wheel, clicking on scrollbar arrows or dynamically with window.scrollTo(left, top)
function.
Is it possible to determine what caused the scroll
event to fire up? Whether it was user intervention or JS code?
1 Answer
Reset to default 7I don't think you can determine what caused the scrolling. The scroll event only indicates that the window is scrolling, not why it's scrolling.
But perhaps you suspend the scroll event listener or set a flag before calling window.scrollTo()
from your code. Here in Safari, if you use scrollTo()
, the scroll event only fires once regardless of how much you scroll, so you could conceivably do something like this:
// somewhere in your code...
isCodedScrollEvent = true;
window.scrollTo(0, 200);
// elsewhere in your code...
function scrollListener(event) {
if( isCodedScrollEvent ) {
// event was caused by code, so handle it differently
// and then flip the flag back to false, so the next
// will be handled normally again
isCodedScrollEvent = false;
} else {
// event was caused by user
}
}
It ain't pretty, but it ought to work