遇到浮动内容时,粘性导航会中断吗?

时间:2019-04-21 06:28:21

标签: html css css-float css-position sticky

出于指导目的,我正在使用最基本的CSS。我有一个设置为position: sticky的导航栏,直到它与页面下方的一组浮动列进行交互时,效果很好。

gif of site scrolling and breaking nav

我不确定这是因为浮动,还是关于布局简单性的一些较差的计算。现在,两列布局的制作方式如下:

#main-menu {
  width: 100%;
  height: auto;
  text-align: right;
  position: sticky;
  position: -webkit-sticky;
  top: 0px;
  z-index: 2;
}
.column {
    width: 50%;
    float: left;
}

使用行内块显示有效,并且不会影响粘性菜单,但是按预期,我无法将其设置为50%的宽度。一个修复程序会很棒,但也可以解释为什么我会遇到这种情况。

谢谢!

#main-menu {
  width: 100%;
  height: auto;
  background-color: #222222;
  line-height: 2em;
  text-align: right;
  position: sticky;
  position: -webkit-sticky; /* Safari */
  top: 0px;
  z-index: 2;
}
.site-name {
  float: left;
  margin: auto 1em;
  padding: 0.5em 1em;
}
#main-menu a {
  color: #FFFFFF;
  text-decoration: none;
}
#main-menu .site-name a:hover {
  color: #00BBBB;
}
.menu-item {
  min-width: 2em;
  text-align: center;
  display: inline-block;
  padding: 0.5em 1em;
}
.menu-item:hover {
  background-color: #00BBBB;
}

#main {
  padding: 2em 4em;
  clear: both;
}
.column-lg {
  width: 50%;
  float: left;
  
}
<h1>Site Above Fold Content</h1>

<nav id="main-menu">
  <div class="site-name">
    <a href="#">Title</a>
  </div>
  <div class="menu-item">
    <a href="#">L1</a>
  </div>
  <div class="menu-item">
    <a href="#">L2</a>
  </div>
</nav>

<h2>Under Fold Content (Before Floated Columns)</h2>
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred.</p>

<br><br>

<h3>BYE-BYE NAV!!</h3>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
  <div class="row">
    <h3>1 Title Impsum Amet</h3>
      <p>Lorem ipsum dolor amet bitters ethical microdosing, narwhal jean shorts venmo umami YOLO 90's trust fund activated charcoal lomo pok pok hammock. Man bun marfa blog narwhal letterpress food truck. Umami forage disrupt, snackwave DIY mlkshk aesthetic kogi bitters vice.</p>
  </div>
  
  <div class="row">
    <h3>2 Title Impsum Amet</h3>
    <p>2 Vegan williamsburg jianbing, gluten-free tote bag try-hard mixtape yuccie +1 everyday carry shabby chic umami vexillologist pop-up edison bulb. Whatever everyday carry listicle, coloring book hell of microdosing gastropub banh mi yuccie tumblr art party. Aesthetic hammock kitsch microdosing, viral biodiesel tumblr cliche beard readymade seitan. Copper mug chambray street art raclette shaman fam neutra.</p>
  </div>
</div>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
   <div class="row">
    <p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred. Intelligentsia heirloom keytar, hot chicken synth tote bag vaporware williamsburg pok pok kickstarter 3 wolf moon selvage hoodie trust fund cronut. Occupy bicycle rights drinking vinegar small batch, vaporware taxidermy flannel live-edge marfa.</p>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

浮动元素不再处于DOM的正常流动中,从而减小了主体的整体高度。使用检查器,您可以看到以下内容(蓝色代表身体的高度):

enter image description here

因此,当您遇到浮标时,相对于车身的position: sticky似乎会滚动。

一个“解决方案”是清除您的浮标。我将以下clearfix应用于正文:

body:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}

这是带有演示的snippet

body:after {
    content: "."; 
    display: block; 
    height: 0; 
    clear: both; 
    visibility: hidden;
}
#main-menu {
  width: 100%;
  height: auto;
  background-color: #222222;
  line-height: 2em;
  text-align: right;
  position: sticky;
  position: -webkit-sticky; /* Safari */
  top: 0px;
  z-index: 2;
}
.site-name {
  float: left;
  margin: auto 1em;
  padding: 0.5em 1em;
}
#main-menu a {
  color: #FFFFFF;
  text-decoration: none;
}
#main-menu .site-name a:hover {
  color: #00BBBB;
}
.menu-item {
  min-width: 2em;
  text-align: center;
  display: inline-block;
  padding: 0.5em 1em;
}
.menu-item:hover {
  background-color: #00BBBB;
}

#main {
  padding: 2em 4em;
  clear: both;
}
.column-lg {
  width: 50%;
  float: left;
}
<h1>Site Above Fold Content</h1>

<nav id="main-menu">
  <div class="site-name">
    <a href="#">Title</a>
  </div>
  <div class="menu-item">
    <a href="#">L1</a>
  </div>
  <div class="menu-item">
    <a href="#">L2</a>
  </div>
</nav>

<h2>Under Fold Content (Before Floated Columns)</h2>
<p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred.</p>

<br><br>

<h3>BYE-BYE NAV!!</h3>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
  <div class="row">
    <h3>1 Title Impsum Amet</h3>
      <p>Lorem ipsum dolor amet bitters ethical microdosing, narwhal jean shorts venmo umami YOLO 90's trust fund activated charcoal lomo pok pok hammock. Man bun marfa blog narwhal letterpress food truck. Umami forage disrupt, snackwave DIY mlkshk aesthetic kogi bitters vice.</p>
  </div>
  
  <div class="row">
    <h3>2 Title Impsum Amet</h3>
    <p>2 Vegan williamsburg jianbing, gluten-free tote bag try-hard mixtape yuccie +1 everyday carry shabby chic umami vexillologist pop-up edison bulb. Whatever everyday carry listicle, coloring book hell of microdosing gastropub banh mi yuccie tumblr art party. Aesthetic hammock kitsch microdosing, viral biodiesel tumblr cliche beard readymade seitan. Copper mug chambray street art raclette shaman fam neutra.</p>
  </div>
</div>

<div class="column-lg">
  <h2>Lorem Ipusm</h2>
   <div class="row">
    <p>Gentrify woke irony +1 tote bag lo-fi drinking vinegar. Bushwick YOLO retro pinterest cloud bread skateboard. Small batch retro twee scenester roof party humblebrag celiac 8-bit direct trade franzen flannel cray. Kogi knausgaard godard selfies umami deep v, woke whatever 8-bit prism cred. Intelligentsia heirloom keytar, hot chicken synth tote bag vaporware williamsburg pok pok kickstarter 3 wolf moon selvage hoodie trust fund cronut. Occupy bicycle rights drinking vinegar small batch, vaporware taxidermy flannel live-edge marfa.</p>
  </div>
</div>