Detect whether scroll event was created by user?

Unfortunately, there is no direct way of telling that.

Unfortunately, there is no direct way of telling that. I would say if you can redesign your app so that it doesn't depend on this type of flow, go for that. If not, a workaround I can think of is to keep track of user initiated scrolls and check that to see if the scroll was triggered by the browser or by the user.

Here's an example that I put together which does this pretty well (except for browsers where jQuery history has problems with). You need to run this locally to be able to test it fully (jsFiddle/jsbin are not good fits as they iFrame the contents). Here's the test cases that I validated: Page loads - userScroll is false Scroll using mouse/keyboard - userScroll becomes true Click on the link to jump to page bottom - userScroll becomes false Click Back/Forward - userScroll becomes false; hello there click here to go down just sitting Notes: This doesn't track scrolling when the user drags the scrollbar with mouse.

This can be added with some more code, which I left as an exercise for you. Event. KeyCodes may vary by OS, so you may have to change that appropriately.

Hope this helps!

Thank you Mrchief. I think this is the best answer even though it is not what I was hoping it would be (What? There's no event.

Thrower property?! ). – mrtsherman Aug 28 at 4:01.

Try using the Mousewheel and DOMMouseScroll events instead. See quirksmode.org/dom/events/scroll.html.

Thank you for this suggestion. However I don't think this has what I want. I actually need to differentiate between browser and user scroll.

Not just target the mousewheel. – mrtsherman Aug 12 at 12:44 I think the mousewheel event is fired before the onscroll event. So you can set var userscroll=true on mousewheel, and detect it in onscroll (and reset it to false).

– Gerben Aug 12 at 18:24 There are a large number of user initiated scroll events that would not be covered though. Arrow keys, window resize, etc. It is much safer to say the sender of this event is "X" than to say everything outside this event must be "X." It also does not work for me regardless since I want to identify a scroll initiated by the browser, not by the user.

So to make this use case I would have to track all mousewheel scrolls and then try to deduce whether the subsequent scroll event was user based on that. I am afraid it is just unworkable. – mrtsherman Aug 12 at 18:50.

More about in Javascript Tutorial - The Scroll Wheel.

Thanks, but this is basically the same as the previous answer. The reasons it won't work are the same. – mrtsherman Aug 24 at 17:34.

As far as I know it is impossible (without any work) to tell whenever scroll event has been issued by "user" or by other means. You could try (as others mentioned) catch mousewheel events, then probably trying to catch keydown event on any keys that can trigger scroll (arrows, space etc. ) while checking what element is currently focused, since you for example can't scroll using arrow keys while typing in an input field.In general that would be complex and messy script. Depending on situation you're dealing with you could I guess "revert the logic", and instead of detecting user issued scroll events just hook in into any scrolls made programatically and treat any scroll events not made by your code as made by an user.

Like I said it depends on a situation, and what you're trying to achive.

Thanks WTK. This was the best answer for awhile, but unfortunately for you Mrchief expounded on your answer with some code examples so I gave the bounty to him. If I could have split it I would have!

– mrtsherman Aug 28 at 4:02 That's ok. It's not about bounty but about spreading and gaining knowledge :) – WTK Aug 29 at 8:20.

You can check the scroll position on ready. When you fire the on scroll event check to make sure the scroll position is different than it was when the page loaded. Lastly be sure to clear out the stored value once the page is scrolled.

$(function () { var loadScrollTop = ($(document).scrollTop() > 0? $(document).scrollTop() : null); $(document). Scroll(function (e) { if ( $(document).scrollTop()!

== loadScrollTop) { // scroll code here! } loadScrollTop = null; }); }).

Thank you for this idea. There are some scenarios that this doesn't quite cover for me. Although it is a good idea and I will keep it in my back pocket if I ever need it.

– mrtsherman Aug 28 at 3:59.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions