Detect if a scroll event is triggered manually in jQuery

后端 未结 7 1082
执念已碎
执念已碎 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:27

    Using @Tony's accepted answer and @DanielTonon's comment I came up with the following solution:

      var animatedScroll = false;
      var lastAnimatedScroll = false;
      $(window).scroll(function(event){
        lastAnimatedScroll = animatedScroll;
        animatedScroll = $('html, body').is(':animated');
      });
    

    This seems to solve the issue mentioned whereby jquery removes the .is(':animated') then scrolls one more pixel, which leads to .is(':animated') ending on a false. By storing the second to last version of .is(':animated') you can be (more) sure whether or not the scroll was an animated one or not.

    When you want to know if the scroll was animated or not just check the lastAnimatedScroll variable.

    This has NOT been thoroughly tested by me but has been correct on many page refreshes so I will assume it works well enough.

    0 讨论(0)
  • 2021-01-11 13:33

    After attempting to implement the various solutions in this issue I came up with a different approach that is working well for me.

    I use a manual boolean for whether an animation is running:

    var isRunningAnimation = false;
    

    and set it to true just before animating, and false in the jQuery animate callback function:

      isRunningAnimation = true;
    
      $('html').animate({
        scrollLeft: 100,
        scrollTop:  100
      }, 400, 'swing', function() {
        isRunningAnimation = false;
      });
    

    and then in the scroll listener just check that boolean:

    $('scroll', function() {
      if (!isRunningAnimation) {
        // If we made it to here, the animation isn't running
      }
    });
    

    Of course technically if the user decides to manually scroll during the animation, that won't trigger the on scroll logic either, but that seems like enough of an edge case to not worry about.

    0 讨论(0)
  • 2021-01-11 13:37

    I would suggest First of all create a javascript function

    // Attaching scroll event when document/window is loaded
        function OnFirstLoad() {
            if (document.attachEvent) {
                document.attachEvent('onscroll', scrollEvent);
            } else if (document.addEventListener) {
                document.addEventListener('scroll', scrollEvent, false);
            }
    
        }
    

    then, use either

            window.onload = OnFirstLoad;
    

    Or

        $(document).ready(function () {
             OnFirstLoad();
        });
    

    In This scroll event is a function

    function scrollEvent(e) {
            var body = document.body,
                 html = document.documentElement;
    
            var docHeight = Math.max(body.scrollHeight, body.offsetHeight,
                                   html.clientHeight, html.scrollHeight, html.offsetHeight);
            var currentScroll = (document.documentElement && document.documentElement.scrollTop) || document.body.scrollTop;
            // implement your logic according to requirement
    
        }
    
    0 讨论(0)
  • 2021-01-11 13:38

    Maybe :animated selector will help you:

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

    Demo

    0 讨论(0)
  • 2021-01-11 13:44

    If you want to bind with jquery selector and check for event

    $('#div.class').bind('scroll mousedown wheel DOMMouseScroll mousewheel keyup touchmove', function (e) {
        if (e.which > 0 || e.type == "mousedown" || e.type == "mousewheel" || e.type == "touchmove") {
          // any code
        }
    })
    
    0 讨论(0)
  • 2021-01-11 13:44
    jQuery(document).on('click', 'p.questions__text a[data-clickid="delay_next_delivery"]', function(ele){
        if(ele.originalEvent.isTrusted){
            // human
        } else {
            // non human
        }
    });
    
    0 讨论(0)
提交回复
热议问题