Sticky Header after scrolling down

前端 未结 10 1156
野性不改
野性不改 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 11:53

    Here's is quite a list of jQuery plugins that will help achieve similar effect: http://jquery-plugins.net/tag/sticky-scroll

    0 讨论(0)
  • 2020-11-27 11:53

    css:

    header.sticky {
      font-size: 24px;
      line-height: 48px;
      height: 48px;
      background: #efc47D;
      text-align: left;
      padding-left: 20px;
    }
    

    JS:

    $(window).scroll(function() {
     if ($(this).scrollTop() > 100){  
        $('header').addClass("sticky");
      }
      else{
        $('header').removeClass("sticky");
      }
    });
    
    0 讨论(0)
  • 2020-11-27 11:55

    I used jQuery .scroll() function to track the event of the toolbar scroll value using scrollTop. I then used a conditional to determine if it was greater than the value on what I wanted to replace. In the below example it was "Results". If the value was true then the results-label added a class 'fixedSimilarLabel' and the new styles were then taken into account.

        $('.toolbar').scroll(function (e) {
    //console.info(e.currentTarget.scrollTop);
        if (e.currentTarget.scrollTop >= 130) {
            $('.results-label').addClass('fixedSimilarLabel');
        }
        else {      
            $('.results-label').removeClass('fixedSimilarLabel');
        }
    });
    

    http://codepen.io/franklynroth/pen/pjEzeK

    0 讨论(0)
  • 2020-11-27 11:59

    Add debouncing, for efficiency http://davidwalsh.name/javascript-debounce-function

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 12:06

    a similar solution using jquery would be:

    $(window).scroll(function () {
      $('.header').css('position','fixed');
    });
    

    This turns the header into a fixed position element immediately on scroll

    0 讨论(0)
提交回复
热议问题