Detect jquery event trigger by user or call by code

javascript, jquery

Solution

$('#scroller').scroll(function(e) {
    if (e.originalEvent) {
        // scroll happen manual scroll
        console.log('scroll happen manual scroll');
    } else {
        // scroll happen by call
        console.log('scroll happen by call');
    }
});

$('#scroller').scroll(); // just a initial call

When you scroll by call the `e.originalEvent` will `undefined` but when scroll manually it will give `scroll` object.

DEMO

Problem

I have some `window.onscroll` event ``` $(window).scroll(function(e){ //My Stuff }); ``` but in my code I call animate scroll to some where ``` $('html, body').stop().animate({ scrollTop:555 }, 1000); ``` so how I detect the page was scroll by user or call by my code. My current solution is put a flag before call `animate` in my code then clear it but it's not clever solution. I've also read about detect `e.which` or `e.originalEvent` but it's not work. I think you expert have a good solution here.

Original source