1

I have a navigation bar placed at the bottom of the browser window, above and outside all div elements. I am trying to get the navbar to stick at the top of the window when the user scrolls and it reaches the top..

I have tried by doing the following however I couldn't seem to get it to work; also tried some other solutions online but had no luck. Has anyone got any ideas ?

      
    $(document).ready(function() {
        // Cache selectors for faster performance.
        var $window = $(window),
            $mainNavBar = $('#navBar'),
            $mainNavBarAnchor = $('#navBarAnchor');
    
        // Run this on scroll events.
        $window.scroll(function() {
            var window_top = $window.scrollTop();
            var div_top = $mainNavBarAnchor.offset().top;
            if (window_top > div_top) {
                // Make the div sticky.
                $mainNavBar.addClass('stick');
                $mainNavBarAnchor.height($mainNavBar.height());
            }
            else {
                // Unstick the div.
                $mainNavBar.removeClass('stick');
                $mainNavBarAnchor.height(0);
            }
        });
 });
    
    nav { height: 75px; width: 100%; position: absolute; bottom: 0; margin 0; padding 0; float: none; background-color: none; border: 0px solid #000; z-index: 100; color: #fff;}

ol { margin: auto; width: auto; height: auto; text-align: center; background-color: none; margin-top: 20px;}
ol li { display: inline-block; font-family: 'dinregular'; font-size: 1.6em; width: auto; margin: auto;  background-color: none; color: #fff }
ol li:nth-child(1) { margin-right: 25px; margin-left: 0; }
ol li:nth-child(2) { margin-right: 25px; margin-left: 0;}
ol li:nth-child(3) { margin-right: 25px; margin-left: 0; }
    
.stick {
  position: fixed;
  top: 0;
  background-color: white; 
 }
    <script src="https://code.jquery.com/jquery-1.11.0.js"></script>


    <div id="navBarAnchor"></div>
    <nav id="navbar">
      <ol>
       <a href="#About"><li>About</li> </a>
       <a href="#Services"><li>Services</li> </a>
       <a href="#Work"><li>Work</li> </a>
       <a href="#Contact"><li>Contact</li> </a>
      </ol>
    </nav> 
RileyDev
  • 2,950
  • 3
  • 26
  • 61
  • 1
    The answer and same question can be found here: http://stackoverflow.com/questions/14667829/how-to-create-a-sticky-navigation-bar-that-becomes-fixed-to-the-top-after-scroll/19144107#19144107 – SoEzPz Mar 20 '17 at 21:34

3 Answers3

1

I think you are looking for maybe something like this:

var header = $("#navbar");
$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  if (scroll >= window.innerHeight) {
    header.addClass("fixed");
  } else {
    header.removeClass("fixed");
  }
});
#navbar.fixed {
  position: fixed;
  top: 0;
}

nav {
  height: 100px;
  width: 100%;
  position: absolute;
  bottom: 0;
  margin 0;
  padding 0;
  float: none;
  background-color: white;
  border: 0px solid #000;
  z-index: 100;
  color: #fff;
}

.container {
  padding-bottom: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<nav id="navbar">
  <ol>
    <a href="#About">
      <li>About</li>
    </a>
    <a href="#Services">
      <li>Services</li>
    </a>
    <a href="#Work">
      <li>Work</li>
    </a>
    <a href="#Contact">
      <li>Contact</li>
    </a>
  </ol>
</nav>

<div class="container">
  What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type
  specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently
  with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using
  Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default
  model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary
  to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of
  the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et
  Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
  The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions
  from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley
  of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
  Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when
  looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page
  editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and
  the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
  College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32
  and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..",
  comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original
  form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
  when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with
  the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted
  by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop
  publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes
  on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock,
  a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem
  Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem
  Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero
  are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's
  standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
  It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Why do we use it? It is a long established
  fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making
  it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over
  the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making
  it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature,
  discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during
  the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from
  "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting
  industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic
  typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to
  using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their
  infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of
  classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through
  the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise
  on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.
  Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. What is Lorem Ipsum? Lorem Ipsum is simply dummy text
  of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries,
  but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker
  including versions of Lorem Ipsum. Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution
  of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many
  web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It
  has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum
  passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in
  45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s
  is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</div>
NickyTheWrench
  • 3,150
  • 1
  • 23
  • 35
0

You can just add/remove class with position: fixed when window.scrollTop is > then offset top of your element or nav in this case. You can also recalculate offset top on resize.

var nav = $('nav'), w = $(window), sTop = nav.offset().top

w.resize(function() {
  sTop = nav.offset().top
})

w.scroll(function() {
  if(w.scrollTop() > sTop) {
    nav.addClass('fixed')
  } else {
    nav.removeClass('fixed')
  }
})
.content {
  height: 100vh;
}
nav {
  width: 100%;
}
ol {
  display: flex;
  justify-content: space-between;
  padding: 0;
  border: 1px solid black;
  margin: 0;
}
body {
  height: 2500px;
  margin: 0;
}
.fixed {
  position: fixed;
  top: 0;
  left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content"></div>
<nav id="navbar">
  <ol>
   <a href="#About"><li>About</li> </a>
   <a href="#Services"><li>Services</li> </a>
   <a href="#Work"><li>Work</li> </a>
   <a href="#Contact"><li>Contact</li> </a>
  </ol>
</nav>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

You first get the position of the navigation so you can get the "trigger" position this will be different on other screen resolutions.

Then you just listen on the scroll event for window and add / remove a class to fix the navigation bar.

Take a look at my jsFiddle : https://jsfiddle.net/xopq0r2h/1/

Html

<div class="content">
  CONTENT 0
</div>
<div class="navigation">
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
</div>
<div class="content">
  CONTENT 1
</div>
<div class="content">
  CONTENT 2
</div>
<div class="content">
  CONTENT 3
</div>

Jquery

$(function()
{
    var navigationYPosition = $('.navigation').position().top;
     $(window).scroll(function () { 
   var $navigation = $('.navigation');
    if ($(window).scrollTop() > 400) {
      $navigation.addClass('fixed-top');
    }
    if ($(window).scrollTop() < 401) {
      $navigation.removeClass('fixed-top');
    }
  });
});

CSS

.navigation{
  margin: 0;
  width: 100%;
  min-height: 32px;
  background-color: #000;
  padding: 5px;
}

.content{
  height: 400px;
  border: solid 1px #f0f;
}

.fixed-top{
  position: fixed;
  top: 0;
}

a{
  margin-left: 3px;
  background-color: #fff;
}
Canvas
  • 5,779
  • 9
  • 55
  • 98