Bootstrap 3: How to affix after 100% height

本秂侑毒 提交于 2019-12-12 16:24:30

问题


I am trying to get my navbar to affix to the top after an intro that is set at 100% height but I am unsure what is the best way to go about doing this?

Code:

CSS
html, body{ height: 100%; min-height: 100%;} 
.intro {height: 100%;text-align: center;}

HTML
<div class="intro">
  <div class="container">
    <h1>Lorem Ipsum</h1>
  </div>
</div>

<nav>
  <div class="container">
    <div class="navbar-header" data-spy="affix">
      <button type="button" class="navbar-toggle"  data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only">Toggle navigation</span>
         <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Test Navbar</a>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    </div><!--/.nav-collapse -->
  </div>
</nav>

EDIT: I am using Bootstrap 3.0.


回答1:


You can use a jQuery function to calc the height of the #intro

$('nav').affix({
      offset: {
        top: $('.intro').height()
      }
}); 

Here's a demo that works in a similar way..

http://bootply.com/106399




回答2:


Give your nav element an id:

<nav id="derp">
  <div class="container">

and then grab the offset of that element:

$('nav').affix({
      offset: {
        top: $('nav#derp').offset().top
      }
}); 

This is a better approach than relying on preceding elements (e.g. the height on the .intro)



来源:https://stackoverflow.com/questions/21174487/bootstrap-3-how-to-affix-after-100-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!