简体   繁体   English

固定位置无效

[英]Position fixed is not working

I'm trying to implement the persistent header from this tutorial: http://css-tricks.com/persistent-headers/ 我正在尝试从本教程中实现持久标头: 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 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 的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来保存标题大小即可防止页面混乱

<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 然后,您只需要一个简单的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 您需要做的就是从Document ready调用函数fixMenu()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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