简体   繁体   中英

Position fixed is not working

I'm trying to implement the persistent header from this tutorial: http://css-tricks.com/persistent-headers/

My Markup:

    <div class="col-md-4 col-sm-4 col-xs-12 postRight persist-area">
       <h2 class="persist-header">rgergergerg</h2>  
       <div class="nom-img">
         <a href="ergeg.html"><img src="img/ergerg.jpg"></a>
       </div>
       <div class="tag-nom postCenter"> <a href="#"><img src="img/erg.png"></a>
         <h4><a href="#">serger</a></h4>
       </div>
    </div>

<div class="col-md-4 col-sm-4 col-xs-12 postRight persist-area">
       <h2 class="persist-header">rgergergerg</h2>  
       <div class="nom-img">
         <a href="ergeg.html"><img src="img/ergerg.jpg"></a>
       </div>
       <div class="tag-nom postCenter"> <a href="#"><img src="img/erg.png"></a>
         <h4><a href="#">serger</a></h4>
       </div>
    </div>

jQuery

var clonedHeaderRow;

       $(".persist-area").each(function() {
           clonedHeaderRow = $(".persist-header", this);
           clonedHeaderRow
             .before(clonedHeaderRow.clone())
             .css("width", clonedHeaderRow.width())
             .addClass("floatingHeader");

       });

       $(window)
        .scroll(UpdateTableHeaders)
        .trigger("scroll");




    function UpdateTableHeaders() {
       $(".persist-area").each(function() {

           var el             = $(this),
               offset         = el.offset(),
               scrollTop      = $(window).scrollTop(),
               floatingHeader = $(".floatingHeader", this)

           if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height())) {
               floatingHeader.css({
                "visibility": "visible"
               });
           } else {
               floatingHeader.css({
                "visibility": "hidden"
               });      
           };
       });
    }

CSS

.floatingHeader {
  position: fixed;
  top: 0;
  visibility: hidden;
}

The problem is that the fixed position is not working even though it is active. The title goes up without being fixed. What might be the problem?

This is really complicated way of doing it. It can be done much more easily

You basicaly need only one extra div to save header size to prevent page scuttering

<div class="persistent">
   <nav>
      <ul>
         <li><a href="#alfa" class="button"><span>ALFA</span></a></li>
      </ul>
   </nav>
</div>

<div class="persistent">
   <nav>
      <ul>
         <li><a href="#beta" class="button"><span>ALFA</span></a></li>
      </ul>
   </nav>
</div>

Then, all you need si simple piece of jQuery

    function fixMenu()
{
  $('.persistent').each(function(){

    var menu_place = $(this).offset().top;
    var menu = $(this).find('nav'); 

    $(window).scroll(function(){

        var scroll_top = $(window).scrollTop(); 

        if ( scroll_top > menu_place )
            {
                menu.css({ 'position': 'fixed', 'top':0 });
            }
        else
            {
                menu.css({ 'position': 'relative' }); 
            }
    });
}

All you need to do is just call function fixMenu() from Document ready

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM