Detect if a scroll event is triggered manually in jQuery

后端 未结 7 1083
执念已碎
执念已碎 2021-01-11 13:21

This question was already asked here a long time ago:

Detect jquery event trigger by user or call by code

But it has never been answered conclusively (or m

相关标签:
7条回答
  • 2021-01-11 13:50

    I don't know how well this works with touch screen devices but this works for me on desktop at least

    $(window).on('mousewheel', function(){
        //code that will only fire on manual scroll input
    });
    
    $(window).scroll(function(){
        //code that will fire on both mouse scroll and code based scroll
    });
    

    I don't think there is a way to only target the animated scroll (the accepted answer didn't work for me).

    UPDATE: Warning!

    Unfortunately, 'mousewheel' doesn't seem to pick up on users who manually grab the scroll bar and drag it or users who use the scroll bar arrow buttons :(

    This still works ok for touch screen devices as their swipes seem to count as mouse scrolls. This isn't a great solution for desktop users though.

    0 讨论(0)
提交回复
热议问题