How can I determine the direction of a jQuery scroll event?

后端 未结 25 1463
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 22:24

I\'m looking for something to this effect:

$(window).scroll(function(event){
   if (/* magic code*/ ){
       // upscroll code
   } else {
      // downscrol         


        
相关标签:
25条回答
  • 2020-11-21 23:14

    Check current scrollTop vs previous scrollTop

    var lastScrollTop = 0;
    $(window).scroll(function(event){
       var st = $(this).scrollTop();
       if (st > lastScrollTop){
           // downscroll code
       } else {
          // upscroll code
       }
       lastScrollTop = st;
    });
    
    0 讨论(0)
  • 2020-11-21 23:15

    In case you just want to know if you scroll up or down using a pointer device (mouse or track pad) you can use the deltaY property of the wheel event.

    $('.container').on('wheel', function(event) {
      if (event.originalEvent.deltaY > 0) {
        $('.result').append('Scrolled down!<br>');
      } else {
        $('.result').append('Scrolled up!<br>');
      }
    });
    .container {
      height: 200px;
      width: 400px;
      margin: 20px;
      border: 1px solid black;
      overflow-y: auto;
    }
    .content {
      height: 300px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class="container">
      <div class="content">
        Scroll me!
      </div>
    </div>
    
    <div class="result">
      <p>Action:</p>
    </div>

    0 讨论(0)
  • 2020-11-21 23:17

    Use this to find the scroll direction. This is only to find the direction of the Vertical Scroll. Supports all cross browsers.

        var scrollableElement = document.getElementById('scrollableElement');
    
        scrollableElement.addEventListener('wheel', findScrollDirectionOtherBrowsers);
    
        function findScrollDirectionOtherBrowsers(event){
            var delta;
    
            if (event.wheelDelta){
                delta = event.wheelDelta;
            }else{
                delta = -1 * event.deltaY;
            }
    
            if (delta < 0){
                console.log("DOWN");
            }else if (delta > 0){
                console.log("UP");
            }
    
        }
    

    Example

    0 讨论(0)
  • 2020-11-21 23:18

    I had problems with elastic scrolling (scroll bouncing, rubber-banding). Ignoring the down-scroll event if close to the page top worked for me.

    var position = $(window).scrollTop();
    $(window).scroll(function () {
        var scroll = $(window).scrollTop();
        var downScroll = scroll > position;
        var closeToTop = -120 < scroll && scroll < 120;
        if (downScroll && !closeToTop) {
            // scrolled down and not to close to top (to avoid Ipad elastic scroll-problems)
            $('.top-container').slideUp('fast');
            $('.main-header').addClass('padding-top');
        } else {
            // scrolled up
            $('.top-container').slideDown('fast');
            $('.main-header').removeClass('padding-top');
        }
        position = scroll;
    });
    
    0 讨论(0)
  • 2020-11-21 23:22

    I understand there has already been an accepted answer, but wanted to post what I am using in case it can help anyone. I get the direction like cliphex with the mousewheel event but with support for Firefox. It's useful doing it this way in case you are doing something like locking scroll and can't get the current scroll top.

    See a live version here.

    $(window).on('mousewheel DOMMouseScroll', function (e) {
    
        var direction = (function () {
    
            var delta = (e.type === 'DOMMouseScroll' ?
                         e.originalEvent.detail * -40 :
                         e.originalEvent.wheelDelta);
    
            return delta > 0 ? 0 : 1;
        }());
    
        if(direction === 1) {
           // scroll down
        }
        if(direction === 0) {
           // scroll up
        }
    });
    
    0 讨论(0)
  • 2020-11-21 23:24

    You Should try this

    var scrl
    $(window).scroll(function(){
            if($(window).scrollTop() < scrl){
                //some code while previous scroll
            }else{
                if($(window).scrollTop() > 200){
                    //scroll while downward
                }else{//scroll while downward after some specific height
                }
            }
            scrl = $(window).scrollTop();
        });
    
    0 讨论(0)
提交回复
热议问题