jQuery - Sticky header that shrinks when scrolling down

前端 未结 6 1562
礼貌的吻别
礼貌的吻别 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:47

    Here a CSS animation fork of jezzipin's Solution, to seperate code from styling.

    JS:

    $(window).on("scroll touchmove", function () {
      $('#header_nav').toggleClass('tiny', $(document).scrollTop() > 0);
    });
    

    CSS:

    .header {
      width:100%;
      height:100px;
      background: #26b;
      color: #fff;
      position:fixed;
      top:0;
      left:0;
      transition: height 500ms, background 500ms;
    }
    .header.tiny {
      height:40px;
      background: #aaa;
    }
    

    http://jsfiddle.net/sinky/S8Fnq/

    On scroll/touchmove the css class "tiny" is set to "#header_nav" if "$(document).scrollTop()" is greater than 0.

    CSS transition attribute animates the "height" and "background" attribute nicely.

提交回复
热议问题