How to fix a header on scroll

后端 未结 12 505
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 11:26

I am creating a header that once scrolled to a certain amount of pixels it fixes and stays in place.

Can I do this using just css and html or do i need jquery too?

相关标签:
12条回答
  • 2020-11-27 12:00

    Hopefully this one piece of an alternate solution will be as valuable to someone else as it was for me.

    Situation:
    In an HTML5 page I had a menu that was a nav element inside a header (not THE header but a header in another element).
    I wanted the navigation to stick to the top once a user scrolled to it, but previous to this the header was absolute positioned (so I could have it overlay something else slightly).
    The solutions above never triggered a change because .offsetTop was not going to change as this was an absolute positioned element. Additionally the .scrollTop property was simply the top of the top most element... that is to say 0 and always would be 0.
    Any tests I performed utilizing these two (and same with getBoundingClientRect results) would not tell me if the top of the navigation bar ever scrolled to the top of the viewable page (again, as reported in console, they simply stayed the same numbers while scrolling occurred).

    Solution
    The solution for me was utilizing

    window.visualViewport.pageTop
    

    The value of the pageTop property reflects the viewable section of the screen, therefore allowing me to track where an element is in reference to the boundaries of the viewable area.
    This allowed a simple function assigned to the scroll event of the window to detect when the top of the navigation bar intersected with the top of the viewable area and apply the styling to make it stick to the top.

    Probably unnecessary to say, anytime I am dealing with scrolling I expect to use this solution to programatically respond to movement of elements being scrolled.
    Hope it helps someone else.

    0 讨论(0)
  • Glorious, Pure-HTML/CSS Solution

    In 2019 with CSS3 you can do this without Javascript at all. I frequently make sticky headers like this:

    body {
      overflow-y: auto;
      margin: 0;
    }
    
    header {
      position: sticky; /* Allocates space for the element, but moves it with you when you scroll */
      top: 0; /* specifies the start position for the sticky behavior - 0 is pretty common */
      width: 100%;
      padding: 5px 0 5px 15px;
      color: white;
      background-color: #337AB7;
      margin: 0;
    }
    
    h1 {
      margin: 0;
    }
    
    div.big {
      width: 100%;
      min-height: 150vh;
      background-color: #1ABB9C;
      padding: 10px;
    }
    <body>
    <header><h1>Testquest</h1></header>
    <div class="big">Just something big enough to scroll on</div>
    </body>

    0 讨论(0)
  • Or just simply add a span tag with the height of the fixed header set as its height then insert it next to the sticky header:

    $(function() {
      var $span_height = $('.fixed-header').height;
      var $span_tag = '<span style="display:block; height:' + $span_height + 'px"></span>';
    
      $('.fixed-header').after($span_tag);
    });
    
    0 讨论(0)
  • 2020-11-27 12:05
     $(document).ready(function(){
    
        var div=$('#header');
        var start=$(div).offset().top;
    
        $.event.add(window,'scroll',function(){
            var p=$(window).scrollTop();
            $(div).css('position',(p>start)?'fixed':'static');
            $(div).css('top',(p>start)?'0px':'');
    
        }); 
    });
    

    It works perfectly.

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

    The simplest way is: HTML:

    <header>
            <h1>Website</h1>
    </header>
    

    CSS:

    header{
        position: sticky;
        top: 0;
    }
    
    0 讨论(0)
  • 2020-11-27 12:06

    I have modified the Coop's answer. Please check the example FIDDLE Here's my edits:

    $(window).scroll(function(){
      if ($(window).scrollTop() >= 330) {
        $('.sticky-header').addClass('fixed');
       }
       else {
        $('.sticky-header').removeClass('fixed');
       }
    });
    
    0 讨论(0)
提交回复
热议问题