Debounce jquery scroll events

后端 未结 3 2034
夕颜
夕颜 2020-12-21 13:01

I just have a general question about debouncing. I have three menus at different positions on the page that when they reach a position 85px from the top of the window on scr

3条回答
  •  盖世英雄少女心
    2020-12-21 13:58

    I would say, rather than trying to set the css directly, take advantage of classes.

    .fixed_heading_shop,
    .fixed_heading_pricetable {
      position:relative;
      top:0;
    }
    .ghost_div0,
    .ghost_div1 {
      display:block;
    }
    .scroll_trick .fixed_heading_shop,
    .scroll_trick .fixed_heading_pricetable {
      position:fixed;
      top:85px;
    }
    .scroll_trick .ghost_div0,
    .scroll_trick .ghost_div1 {
      display:none;
    }
    
    $(function(){
      var $window = $(window);
      var $body = $('body');
      var top = $('.fixed_heading_shop').offset().top-85;
    
      $window.scroll(function(){
        if( $window.scrollTop() > top) {
          $body.addClass('scroll_trick');
        } else {
          $body.removeClass('scroll_trick');
        }   
      });
    });
    

提交回复
热议问题