Header changes as you scroll down (jQuery)

后端 未结 7 1495
滥情空心
滥情空心 2020-12-01 01:22

TechCrunch recently redesigned their site and they have a sweet header that minifies into a thinner version of their branding as you scroll down.

You can see what I

7条回答
  •  情深已故
    2020-12-01 02:12

    It's not too hard, it's just a simple .scroll() event. I can't seem to do it in fiddle because of the way the panels are positioned Check EDIT!. But basically what you have is have a div on the top that is position: absolute so it's always on top, then use .scroll()

    $("body").scroll( function() {
        var top = $(this).scrollTop();
        // if statement to do changes
    });
    

    The scrollTop() method is used to determine how much the body has scrolled.

    And depending on where you would like to change your header div, you can have your if statement do many things. In the case of the example you provided, it would be something like

    if ( top > 147 )
        // add the TechCrunch div inside the header
    else
        // hide the TechCrunch div so that space is transparent and you can see the banner
    

    EDIT

    Yay! I was able to make this fiddle to demonstrate the example! :)

    Good luck!

提交回复
热议问题