简体   繁体   中英

Navbar not staying fixed

Hi guys I am trying to follow this Example and do exactly the same for my site, however I cant get my nav bar to work and follow the screen down. I have tried a lot and not sure what is going on.

HTML:

<!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:

    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;
    }

If I change var navHeight = $( window ).height() - 70; to 277 it works fine however if I make the screen smaller it snaps straight up without waiting so its really weird.

Any help on this matter would be great.

Thanks

Apologies. I tried to fix your code instead of giving you a different solution. This works without the jumping.

var $window = $(window),
        $el = $('nav'),
        elTop = $el.offset().top;

      $window.scroll(function() {
        $el.toggleClass('navbar-fixed-top', $window.scrollTop() > elTop);
      });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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