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

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

    You can use both scroll and mousewheel option to track up and down movement at once.

     $('body').bind('scroll mousewheel', function(event) {
    
    if (event.originalEvent.wheelDelta >= 0) {
          console.log('moving down');   
        }
        else {
          console.log('moving up'); 
        }
    });
    

    You can replace 'body' with (window) as well.

提交回复
热议问题