【问题标题】:How to keep the header static, always on top while scrolling?如何保持标题静态,滚动时始终位于顶部?
【发布时间】:2010-08-29 05:05:24
【问题描述】:

如何防止我的header 与页面的其余部分一起滚动?我考虑过使用frame-setsiframes,只是想知道是否有更简单、更用户友好的方式,这样做的最佳实践是什么?

【问题讨论】:

标签: javascript html css


【解决方案1】:

注意:此答案可追溯到 2010 年。请考虑 2021 年的 position: sticky,如 another answer 中所述。


在包含您的标头的div 上使用position: fixed,类似于

#header {
  position: fixed;
}

#content {
  margin-top: 100px;
}

在本例中,当#content 开始于#header 下方100 像素时,但随着用户滚动,#header 保持在原位。当然,不用说,您需要确保#header 具有背景,以便在两个div 重叠时其内容实际上是可见的。在这里查看position 属性:http://reference.sitepoint.com/css/position

【讨论】:

  • 根据您的divs 之间的关系,您可能需要将margin-top: -100px; 添加到#header 以将其取回您想要的位置。
  • 你知道一种让它滚动直到它到达顶部然后“位置:固定;”的方法吗?如果您的标题从顶部下方开始?
  • 它不包含在我的 DIV 宽度内。
  • 此解决方案需要定义硬编码大小。有没有办法在没有硬编码大小的情况下做到这一点?也许通过将一个 div 定义为低于另一个?在没有硬编码值的情况下,标题和内容之间可能存在某种关系?
  • @AlikElzin-kilaka 不是真的,不。将一个元素设置为position: fixed 可以有效地将其从流中取出,这意味着对于其他元素它不再占用任何空间。你要找的是position: sticky,遗憾的是它还没有广泛的浏览器支持
【解决方案2】:

position: sticky;

modern, supported browsers 中,您可以简单地在 CSS 中使用 -

header {
  position: sticky;
  top: 0;
}

在大多数情况下,它比使用position: fixed 更好,因为position: fixed 将元素从常规元素流中取出。

注意

  1. HTML 结构在使用position: sticky 时很重要,因为它使元素相对于父元素具有粘性。并且粘性定位可能不适用于在父级中设置为粘性的单个元素。
  2. 粘性元素的父元素不应设置overflow 属性例如:如果父元素具有overflow: autooverflow: hidden,则粘性定位可能不起作用
  3. 至少提供topbottomleftright 之一很重要,因为它会使元素相对于某个位置具有粘性。

运行下面的 sn-p 以检查示例实现。

main {
  padding: 0;
}
header {
  position: sticky;
  top:0;
  padding:40px;
  background: lightblue;
  text-align: center;
}

content > div {
  height: 50px;
}
<main>
  <header>
    This is my header
  </header>
  <content>
    <div>Some content 1</div>
    <div>Some content 2</div>
    <div>Some content 3</div>
    <div>Some content 4</div>
    <div>Some content 5</div>
    <div>Some content 6</div>
    <div>Some content 7</div>
    <div>Some content 8</div>
  </content>
</main>

【讨论】:

【解决方案3】:

.header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 88px;
  z-index: 10;
  background: #eeeeee;
  -webkit-box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12);
  -moz-box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12);
  box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12);
}

.header__content-text {
  text-align: center;
  padding: 15px 20px;
}

.page__content-container {
  margin: 100px auto;
  width: 975px;
  padding: 30px;
}
<div class="header">
  <h1 class="header__content-text">
    Header content will come here
  </h1>
</div>
<div class="page__content-container">
  <div style="height:600px;">
    <a href="http://imgur.com/k9hz3">
      <img src="http://i.imgur.com/k9hz3.jpg" title="Hosted by imgur.com" alt="" />
    </a>
  </div>
  <div style="height:600px;">
    <a href="http://imgur.com/TXuFQ">
      <img src="http://i.imgur.com/TXuFQ.jpg" title="Hosted by imgur.com" alt="" />
    </a>
  </div>
</div>

【讨论】:

    【解决方案4】:

    如果你可以使用 bootstrap3 那么你可以使用 css "navbar-fixed-top" 您还需要在 css 下面添加以将您的页面内容向下推送

    body{
    
       margin-top:100px;
    }
    

    【讨论】:

      【解决方案5】:

      这是一个带有 css + jquery (javascript) 的解决方案。

      这里是演示链接Demo

      //html
      
      <div id="uberbar">
          <a href="#top">Top of Page</a>
          <a href="#bottom">Bottom of Page</a>
      
      </div>
      
      //css 
      
      #uberbar    { 
          border-bottom:1px solid #eb7429; 
          background:#fc9453; 
          padding:10px 20px; 
          position:fixed; 
          top:0; 
          left:0; 
          z-index:2000; 
          width:100%;
      }
      
      //jquery
      
      $(document).ready(function() {
          (function() {
              //settings
              var fadeSpeed = 200, fadeTo = 0.5, topDistance = 30;
              var topbarME = function() { $('#uberbar').fadeTo(fadeSpeed,1); }, topbarML = function() { $('#uberbar').fadeTo(fadeSpeed,fadeTo); };
              var inside = false;
              //do
              $(window).scroll(function() {
                  position = $(window).scrollTop();
                  if(position > topDistance && !inside) {
                      //add events
                      topbarML();
                      $('#uberbar').bind('mouseenter',topbarME);
                      $('#uberbar').bind('mouseleave',topbarML);
                      inside = true;
                  }
                  else if (position < topDistance){
                      topbarME();
                      $('#uberbar').unbind('mouseenter',topbarME);
                      $('#uberbar').unbind('mouseleave',topbarML);
                      inside = false;
                  }
              });
          })();
      });
      

      【讨论】:

        【解决方案6】:

        看了所有的答案,我发现一个稍微不同的方式,最小的CSS和没有JS,只有标题的高度需要在#content中正确设置,本例为60px

        CSS:

        #header {
          position: fixed;
          width: 100%;
          top: 0;
          z-index: 10;
        }
        #content {
          margin-top: 60px;
          z-index:1;
        }
        

        HTML:

        <body>
          <div id="header" style="background-color:GRAY; text-align:center; border-bottom:1px SOLID BLACK; color:WHITE; line-height:50px; font-size:40px">
             My Large Static Header
          </div>
          <div id="content">
             <!-- All page content here -->
          </div>
        </body>
        

        【讨论】:

          【解决方案7】:

          除了使用定位和填充/边距并且不知道标题的大小之外,还有一种方法可以通过播放滚动来保持标题固定。

          查看带有固定标头的this plunker

          <html lang="en" style="height: 100%">
          <body style="height: 100%">
          <div style="height: 100%; overflow: hidden">
            <div>Header</div>
            <div style="height: 100%; overflow: scroll">Content - very long Content...
          

          这里的关键是height: 100%overflow 的混合。

          查看从标题here 移除滚动条的具体问题并回答here

          【讨论】:

          • 一个有问题的解决方案。似乎内容的底部被推到了窗口下方。如果您将标题设置得更高,您可以更清楚地看到它。知道如何解决这个问题吗?
          • 这似乎工作正常,但在我的 iPhone5 上滚动变得非常慢。
          • @AlainZelink - 你在 iPhone5 上使用什么浏览器应用程序?
          【解决方案8】:

          我个人需要一个始终可见左侧和顶部标题的表格。受几篇文章的启发,我认为我有一个很好的解决方案,您可能会觉得有帮助。 此版本没有其他解决方案在浮动 div 或列和行的灵活/自动调整大小方面存在的换行问题。

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
              <title></title>
              <script language="javascript" type="text/javascript" src="/Scripts/jquery-1.7.2.min.js"></script>
              <script language="javascript" type="text/javascript">
                  // Handler for scrolling events
                  function scrollFixedHeaderTable() {
                      var outerPanel = $("#_outerPanel");
                      var cloneLeft = $("#_cloneLeft");
                      var cloneTop = $("#_cloneTop");
                      cloneLeft.css({ 'margin-top': -outerPanel.scrollTop() });
                      cloneTop.css({ 'margin-left': -outerPanel.scrollLeft() });
                  }
          
                  function initFixedHeaderTable() {
                      var outerPanel = $("#_outerPanel");
                      var innerPanel = $("#_innerPanel");
                      var clonePanel = $("#_clonePanel");
                      var table = $("#_table");
                      // We will clone the table 2 times: For the top rowq and the left column. 
                      var cloneLeft = $("#_cloneLeft");
                      var cloneTop = $("#_cloneTop");
                      var cloneTop = $("#_cloneTopLeft");
                      // Time to create the table clones
                      cloneLeft = table.clone();
                      cloneTop = table.clone();
                      cloneTopLeft = table.clone();
                      cloneLeft.attr('id', '_cloneLeft');
                      cloneTop.attr('id', '_cloneTop');
                      cloneTopLeft.attr('id', '_cloneTopLeft');
                      cloneLeft.css({
                          position: 'fixed',
                          'pointer-events': 'none',
                          top: outerPanel.offset().top,
                          'z-index': 1 // keep lower than top-left below
                      });
                      cloneTop.css({
                          position: 'fixed',
                          'pointer-events': 'none',
                          top: outerPanel.offset().top,
                          'z-index': 1 // keep lower than top-left below
                      });
                      cloneTopLeft.css({
                          position: 'fixed',
                          'pointer-events': 'none',
                          top: outerPanel.offset().top,
                          'z-index': 2 // higher z-index than the left and top to make the top-left header cell logical
                      });
                      // Add the controls to the control-tree
                      clonePanel.append(cloneLeft);
                      clonePanel.append(cloneTop);
                      clonePanel.append(cloneTopLeft);
                      // Keep all hidden: We will make the individual header cells visible in a moment
                      cloneLeft.css({ visibility: 'hidden' });
                      cloneTop.css({ visibility: 'hidden' });
                      cloneTopLeft.css({ visibility: 'hidden' });
                      // Make the lef column header cells visible in the left clone
                      $("#_cloneLeft td._hdr.__row").css({
                          visibility: 'visible',
                      });
                      // Make the top row header cells visible in the top clone
                      $("#_cloneTop td._hdr.__col").css({
                          visibility: 'visible',
                      });
                      // Make the top-left cell visible in the top-left clone
                      $("#_cloneTopLeft td._hdr.__col.__row").css({
                          visibility: 'visible',
                      });
                      // Clipping. First get the inner width/height by measuring it (normal innerWidth did not work for me)
                      var helperDiv = $('<div style="positions: absolute; top: 0; right: 0; bottom: 0; left: 0; height: 100%;"></div>');
                      outerPanel.append(helperDiv);
                      var innerWidth = helperDiv.width();
                      var innerHeight = helperDiv.height();
                      helperDiv.remove(); // because we dont need it anymore, do we?
                      // Make sure all the panels are clipped, or the clones will extend beyond them
                      outerPanel.css({ clip: 'rect(0px,' + String(outerPanel.width()) + 'px,' + String(outerPanel.height()) + 'px,0px)' });
                      // Clone panel clipping to prevent the clones from covering the outerPanel's scrollbars (this is why we use a separate div for this)
                      clonePanel.css({ clip: 'rect(0px,' + String(innerWidth) + 'px,' + String(innerHeight) + 'px,0px)'   });
                      // Subscribe the scrolling of the outer panel to our own handler function to move the clones as needed.
                      $("#_outerPanel").scroll(scrollFixedHeaderTable);
                  }
          
          
                  $(document).ready(function () {
                      initFixedHeaderTable();
                  });
          
              </script>
              <style type="text/css">
                  * {
                      clip: rect font-family: Arial;
                      font-size: 16px;
                      margin: 0;
                      padding: 0;
                  }
          
                  #_outerPanel {
                      margin: 0px;
                      padding: 0px;
                      position: absolute;
                      left: 50px;
                      top: 50px;
                      right: 50px;
                      bottom: 50px;
                      overflow: auto;
                      z-index: 1000;
                  }
          
                  #_innerPanel {
                      overflow: visible;
                      position: absolute;
                  }
          
                  #_clonePanel {
                      overflow: visible;
                      position: fixed;
                  }
          
                  table {
                  }
          
                  td {
                      white-space: nowrap;
                      border-right: 1px solid #000;
                      border-bottom: 1px solid #000;
                      padding: 2px 2px 2px 2px;
                  }
          
                  td._hdr {
                      color: Blue;
                      font-weight: bold;
                  }
                  td._hdr.__row {
                      background-color: #eee;
                      border-left: 1px solid #000;
                  }
                  td._hdr.__col {
                      background-color: #ddd;
                      border-top: 1px solid #000;
                  }
              </style>
          </head>
          <body>
              <div id="_outerPanel">
                  <div id="_innerPanel">
                      <div id="_clonePanel"></div>
                      <table id="_table" border="0" cellpadding="0" cellspacing="0">
                          <thead id="_topHeader" style="background-color: White;">
                              <tr class="row">
                                  <td class="_hdr __col __row">
                                      &nbsp;
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                                  <td class="_hdr __col">
                                      TOP HEADER
                                  </td>
                              </tr>
                          </thead>
                          <tbody>
                              <tr class="row">
                                  <td class="_hdr __row">
                                      MY HEADER COLUMN:
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                              </tr>
                              <tr class="row">
                                  <td class="_hdr __row">
                                      MY HEADER COLUMN:
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                                  <td class="col">
                                      The quick brown fox jumps over the lazy dog.
                                  </td>
                              </tr>
                          </tbody>
                      </table>
                  </div>
                  <div id="_bottomAnchor">
                  </div>
              </div>
          </body>
          </html>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-07-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多