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

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

    this code work fine with IE, Firefox, Opera and Chrome:

    $(window).bind('wheel mousewheel', function(event) {
          if (event.originalEvent.deltaY >= 0) {
              console.log('Scroll down');
          }
          else {
              console.log('Scroll up');
          }
      });
    

    'wheel mousewheel' and the property deltaY must be use in bind() function.

    Remember : You're user must update their system and browsers for security reasons. In 2018, the excuses of "I have IE 7" is a nonsense. We must educate users.

    Have a good day :)

提交回复
热议问题