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

后端 未结 25 1459
爱一瞬间的悲伤
爱一瞬间的悲伤 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:13

    var tempScrollTop, currentScrollTop = 0; 
    
    $(window).scroll(function(){ 
    
       currentScrollTop = $("#div").scrollTop(); 
    
       if (tempScrollTop > currentScrollTop ) {
           // upscroll code
       }
      else if (tempScrollTop < currentScrollTop ){
          // downscroll code
      }
    
      tempScrollTop = currentScrollTop; 
    } 
    

    or use the mousewheel extension, see here.

提交回复
热议问题