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.
$('button'). click(function(event, wasTriggered) { if (wasTriggered) { alert('triggered in code'); } else { alert('triggered by mouse'); } }); $('button').
To check if event is triggered by a human with JavaScript, we can use the isTrusted property. to check if the event is triggered by a human in our event handler callback. e is the event object and it had the isTrusted property. If isTrusted is true , then the event is triggered by a human.
jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.
$('#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
ive reasked this question and got 2 helpful answers.
i'll link the question here for others who'll find this thread.
Detect if a scroll event is triggered manually in jQuery
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With