Sticky Header after scrolling down

前端 未结 10 1227
野性不改
野性不改 2020-11-27 11:19

I saw this sticky header on this website: http://dunked.com/ (no longer active, view archived site)

When you scroll down the sticky header comes down from t

10条回答
  •  感动是毒
    2020-11-27 12:06

    Here is a JS fiddle http://jsfiddle.net/ke9kW/1/

    As the others say, set the header to fixed, and start it with display: none

    then jQuery

    $(window).scroll(function () {
      if ( $(this).scrollTop() > 200 && !$('header').hasClass('open') ) {
        $('header').addClass('open');
        $('header').slideDown();
       } else if ( $(this).scrollTop() <= 200 ) {
        $('header').removeClass('open');
        $('header').slideUp();
      }
    });
    

    where 200 is the height in pixels you'd like it to move down at. The addition of the open class is to allow us to run an elseif instead of just else, so some of the code doesn't unnecessarily run on every scrollevent, save a lil bit of memory

提交回复
热议问题