首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >纳瓦巴不固定

纳瓦巴不固定
EN

Stack Overflow用户
提问于 2017-05-16 23:40:41
回答 1查看 388关注 0票数 1

嗨,伙计们,我正试图跟踪这个示例,并为我的网站做完全一样的工作,但我不能让我的导航条工作,并跟随屏幕下来。我试了很多次,不知道发生了什么事。

HTML:

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/custom.css" rel="stylesheet">

  </head>
  <body>
    <div class="parallax">
        <div class="avater" style="background-image: url('Images/Pic1.jpg');background-size: cover;"></div>
            <div class="info1">
                <h1>Hero</h1>
                <h2>Play play play</h2>
         </div>
    </div>
     <nav class="navbar navbar-default">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">Section 1</a></li>
            <li><a href="#contact">Section 2</a></li>
            <li><a href="#contact">Section 3</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
    
    <div class="section">
    <h1> Section 1 <h1>
    </div>      
    
    <div class="section2">
    <h1> Section 2 <h1>
    </div>      
    
        <div class="section3">
    <h1> Section 3 <h1>
    </div>      
    
    


    
    
    
    
    
    
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <script>
   $(document).ready(function(){
       $(window).bind('scroll', function() {
       var navHeight = $( window ).height() - 70;
             if ($(window).scrollTop() > navHeight) {
                 $('navbar').addClass('fixed');
             }
             else {
                 $('navbar').removeClass('fixed');
             }
        });
    });
</script>
  </body>
</html>

CSS:

代码语言:javascript
复制
    body {
        font-family: 'Open Sans', sans-serif, Arial;
        font-size: 16px;
        color: #6D6D6D;
        background-color: #F5F5F5;
        font-weight: 300;
    }
    
    h1,h2,h3,h4,h5,h6 {
        font-family: 'Montserrat', sans-serif, Arial;
        padding-bottom: 5px;
        color: #373432;
        line-height: 1em;
        font-weight: normal;
    }
    
    a {
        text-decoration: none;
        outline: none;
    }
    
    /* Header  */
    .parallax {
        background-image: url("../Images/back1.jpg");
        min-height: 700px;
        background-attachment: fixed;
        background-position: center;
        background-repeat: no-repeat;
        background-size: cover;
        padding: 100px 20px;
    }
    
    .avater {
        width: 260px;
        height: 260px;
        border-radius: 50%;
        position: relative;
        cursor: default;
        box-shadow: inset 0 0 0 0 rgba(200, 95, 66, 0.4), inset 0 0 0 16px rgba(255, 255, 255, 0.16), 0 1px 2px rgba(0, 0, 0, 0.1);
        margin: 0 auto;
        
    }
    
    .info1 {
        text-align: center;
        margin-top: 40px;
    }
    
    .info1 > h1 {
        font-size: 60px;
        color: #FFFFFF;
    }
    
    .info1 > h2 {
        color: #FFFFFF;
        font-size: 30px;
    }
    
    
    /* Navbar  */
    
.fixed {
    position: fixed; 
    top: 0; 
    height: 70px; 
    z-index: 1;
}
.navbar {
    margin-bottom: 0px;
    left: 0;
    top: 0;
    width: 100%;
    list-style: none;
    height: 70px; 
}
.navbar-nav {
    float: none;
    margin: 0;
    text-align: center;
}
.navbar-nav > li {
    float: none;
    display: inline-block;
}
.navbar-nav > li > a {
    line-height: 38px;
}
.navbar-nav > li > a.active {
    background-color: #E7E7E7;
}
.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
    color: #333333;
    background-color: #E7E7E7;
}

.navbar-toggle {
    background-color: #000000;
    background-image: none;
    border-radius: 4px;
    float: right;
    margin-bottom: 8px;
    margin-right: 15px;
    margin-top: 18px;
    padding: 9px 10px;
    position: relative;
    
}
.navbar-inverse .navbar-toggle .icon-bar {
    background-color: #2DCC70;
}
.navbar-default .navbar-toggle:hover,
.navbar-default .navbar-toggle:focus {
    background-color: #000000;
}
.navbar-inverse .navbar-collapse,
.navbar-inverse .navbar-form {
    border-color: transparent;
}
    
    .section{
        background-color:Red;
        height: 1000px;
        margin: 0;
    }
    
    .section h1 {
        margin: 0;
    }
    
    .section2{
        background-color:blue;
        height: 1000px;
            margin: 0;
    }
    
    .section3{
        background-color:Black;
        height: 1000px;
            margin: 0;
    }

如果我将var navHeight = $( window ).height() - 70;更改为277,它可以正常工作,但是如果我使屏幕变小,它就会在没有等待的情况下直接打开,所以这真的很奇怪。

在这件事上任何帮助都是很好的。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-16 23:44:27

很抱歉。我试图修复您的代码,而不是给您提供不同的解决方案。不需要跳跃就行了。

代码语言:javascript
复制
var $window = $(window),
        $el = $('nav'),
        elTop = $el.offset().top;

      $window.scroll(function() {
        $el.toggleClass('navbar-fixed-top', $window.scrollTop() > elTop);
      });
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44013287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档