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

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

    You can use this as well

    $(document).ready(function(){
    
      var currentscroll_position = $(window).scrollTop();
    $(window).on('scroll', function(){
    Get_page_scroll_direction();
    });
    
    function Get_page_scroll_direction(){
      var running_scroll_position = $(window).scrollTop();
      if(running_scroll_position > currentscroll_position) {
          
          $('.direction_value').text('Scrolling Down Scripts');
    
      } else {
           
           $('.direction_value').text('Scrolling Up Scripts');
    
      }
      currentscroll_position = running_scroll_position;
    }
    
    });
    .direction_value{
      position: fixed;
      height: 30px;
      background-color: #333;
      color: #fff;
      text-align: center;
      z-index: 99;
      left: 0;
      top: 0;
      width: 100%;
    }
    
    
    

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi ducimus expedita facilis architecto fugiat veniam natus suscipit amet beatae atque, enim recusandae quos, magnam, perferendis accusamus cumque nemo modi unde!

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi ducimus expedita facilis architecto fugiat veniam natus suscipit amet beatae atque, enim recusandae quos, magnam, perferendis accusamus cumque nemo modi unde!

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi ducimus expedita facilis architecto fugiat veniam natus suscipit amet beatae atque, enim recusandae quos, magnam, perferendis accusamus cumque nemo modi unde!

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi ducimus expedita facilis architecto fugiat veniam natus suscipit amet beatae atque, enim recusandae quos, magnam, perferendis accusamus cumque nemo modi unde!

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi ducimus expedita facilis architecto fugiat veniam natus suscipit amet beatae atque, enim recusandae quos, magnam, perferendis accusamus cumque nemo modi unde!

    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nisi ducimus expedita facilis architecto fugiat veniam natus suscipit amet beatae atque, enim recusandae quos, magnam, perferendis accusamus cumque nemo modi unde!

提交回复
热议问题