Custom sticky navigation bar is very choppy

313 views Asked by At

I am trying to make my navigation bar stick to the top using jQuery, and I have succeeded for the most part.

When I run the code on my local web server I get the effect I am looking for. That is, having a 15px top margin before and returning to scrolling, and having the navigation bar stick to the top when scrolling down. However, it does not look seamless and is a bit choppy when I scroll down fast using my scrolling wheel.

I am using Bootstrap version 3.3.4, and jQuery version 1.11.3.

index.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">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Example Brand</title>
    <link href="/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/custom/css/index.css" rel="stylesheet">
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <div class="container">
      <nav class="navbar navbar-default">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse" 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>
            <a class="navbar-brand" href="/">Example Brand</a>
          </div>
          <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
              <li class="active"><a href="/">Home</a></li>
              <li><a href="#">About</a></li>
              <li><a href="#">Contact</a></li>
            </ul>
          </div>
        </div>
      </nav>
    </div>
    <div class="container">
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
      <div class="jumbotron">
        <span>This is here to add some scrolling space</span>
      </div>
    </div>
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="/bootstrap/js/bootstrap.min.js"></script>
    <script src="/custom/js/index.js"></script>
  </body>
</html>

index.js

// On page load function
$(document).ready(function () {

  /* Make the top navbar sticky */

  // Starting navbar top offset
  var navTop = $('.navbar').offset().top;
  // On window scroll function
  $(window).scroll(function () {
    // Scrollbar top offset
    var scrollTop = $(window).scrollTop();
    // If there is an offset difference
    if (scrollTop > navTop) {
      // // Set the navbar top offset as the difference
      $('.navbar').css('top', scrollTop - navTop);
    }
    // If there is not an offset difference
    else {
      // // Set the navbar top offset as 0
      $('.navbar').css('top', 0);
    }
  });
});

index.css

/* Navbar styling */
.navbar {
  /* Starting space between navbar and top of page */
  /* This space will go away after scrolling down */
  margin-top: 15px;
  /* High z index so the navbar renders on top */
  z-index: 100;
}

Also, here is a JsFiddle page running this code. Chrome shows the issue.

Any help, solutions, or alternative suggestions that you could give me would be greatly appreciated. Thank you!

0

There are 0 answers