jQuery - Sticky header that shrinks when scrolling down

前端 未结 6 1559
礼貌的吻别
礼貌的吻别 2020-11-28 00:58

I wonder how to make a sticky header shrink(with animation) when you scroll down the page and goes back to normal state when the page is scrolled up to the top. Here are two

6条回答
  •  粉色の甜心
    2020-11-28 01:43

    Based on twitter scroll trouble (http://ejohn.org/blog/learning-from-twitter/).

    Here is my solution, throttling the js scroll event (usefull for mobile devices)

    JS:

    $(function() {
        var $document, didScroll, offset;
        offset = $('.menu').position().top;
        $document = $(document);
        didScroll = false;
        $(window).on('scroll touchmove', function() {
          return didScroll = true;
        });
        return setInterval(function() {
          if (didScroll) {
            $('.menu').toggleClass('fixed', $document.scrollTop() > offset);
            return didScroll = false;
          }
        }, 250);
      });
    

    CSS:

    .menu {
      background: pink;
      top: 5px;
    }
    
    .fixed {
      width: 100%;
      position: fixed;
      top: 0;
    }
    

    HTML:

    
    

    http://codepen.io/anon/pen/BgqHw

提交回复
热议问题