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

后端 未结 25 1469
爱一瞬间的悲伤
爱一瞬间的悲伤 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: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!
    '); } else { $('.result').append('Scrolled up!
    '); } });
    .container {
      height: 200px;
      width: 400px;
      margin: 20px;
      border: 1px solid black;
      overflow-y: auto;
    }
    .content {
      height: 300px;
    }
    
    
    
    Scroll me!

    Action:

提交回复
热议问题