sdfs sdfs - 2 months ago 7
Javascript Question

How to fix the navigation bar on top on scrolling

Ok, so i want to fix the navigation bar on top scrolling just like this website - http://manipal.edu/
Ok i know, their are many questions similar to this, but i am unable to apply them in my code. Please help

This is my fiddle

HTML:

<header class="topbg">
<img src="xavierslogo.png" alt="St. Xavier's college logo" class="xavierslogo">
<img src="xavierstext.png" alt="St. Xavier's college logo" class="xavierstext">
</header>


<ul class="navbar">
<li><a href="#home">Home</a></li>
<li><a href="#news">Text one</a></li>
<li><a href="#news">Text Two</a></li>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Dropdown ▼</a>
<div class="dropdown-content" id="myDropdown">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</li>

</ul>

<div class="main">


z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>z<br>

</div>


CSS:

body {
width: 100%;
height: 100%;
margin:0px;

}


.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #f2f2f2;

}

.navbar li {
float: left;

}

li a, .dropbtn {
display: inline-block;
color: #333;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.navbar li a:hover, .dropdown:hover .dropbtn {
background-color: #333;
color: #f2f2f2;
}

.navbar li.dropdown {
display: inline-block;
}

.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}

.dropdown-content a:hover {background-color: #333}

.navbar .show {display:block;}
.xavierslogo {
width: 110px;
height: 109px;
margin-left: 20%;

}

.xavierstext {
width: ;
height: ;
margin-left: 0%;

}


.topbg {

background-color: #333;


}

Answer

Try this one...

you have to use jQuery for this snippet.

$(document).ready(function() {
  $(window).scroll(function() {
    if ($(window).scrollTop() > $('header').height())
      $('.navbar').addClass('topNav');
    else
      $('.navbar').removeClass('topNav');
  });
});
body {
  width: 100%;
  height: 100%;
  margin: 0px;
}
.navbar {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #f2f2f2;
}
.navbar li {
  float: left;
}
li a,
.dropbtn {
  display: inline-block;
  color: #333;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
.navbar li a:hover,
.dropdown:hover .dropbtn {
  background-color: #333;
  color: #f2f2f2;
}
.navbar li.dropdown {
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}
.dropdown-content a:hover {
  background-color: #333
}
.navbar .show {
  display: block;
}
.xavierslogo {
  width: 110px;
  height: 109px;
  margin-left: 20%;
}
.xavierstext {
  width: ;
  height: ;
  margin-left: 0%;
}
.topbg {
  background-color: #333;
}
.topNav {
  position: fixed;
  top: 0;
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<header class="topbg">
  <img src="xavierslogo.png" alt="St. Xavier's college logo" class="xavierslogo">
  <img src="xavierstext.png" alt="St. Xavier's college logo" class="xavierstext">
</header>


<ul class="navbar">
  <li><a href="#home">Home</a>
  </li>
  <li><a href="#news">Text one</a>
  </li>
  <li><a href="#news">Text Two</a>
  </li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Dropdown ▼</a>
    <div class="dropdown-content" id="myDropdown">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>

</ul>

<div class="main">


  z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>z
  <br>

</div>

If you want this in plain/pure javascript

function topScrl() {
  if (document.getElementById('body1').scrollTop > document.getElementById('head1').clientHeight)
    document.getElementById('nav1').className = "topNav navbar";
  else
    document.getElementById('nav1').className = "navbar";
}
body {
  width: 100%;
  height: 100%;
  margin: 0px;
}
.navbar {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: #f2f2f2;
}
.navbar li {
  float: left;
}
li a,
.dropbtn {
  display: inline-block;
  color: #333;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
.navbar li a:hover,
.dropdown:hover .dropbtn {
  background-color: #333;
  color: #f2f2f2;
}
.navbar li.dropdown {
  display: inline-block;
}
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}
.dropdown-content a:hover {
  background-color: #333
}
.navbar .show {
  display: block;
}
.xavierslogo {
  width: 110px;
  height: 109px;
  margin-left: 20%;
}
.xavierstext {
  width: ;
  height: ;
  margin-left: 0%;
}
.topbg {
  background-color: #333;
}
.topNav {
  position: fixed;
  top: 0;
  width: 100%;
}
<body id="body1" onscroll="topScrl()">
  <header id="head1" class="topbg">
    <img src="xavierslogo.png" alt="St. Xavier's college logo" class="xavierslogo">
    <img src="xavierstext.png" alt="St. Xavier's college logo" class="xavierstext">
  </header>


  <ul id="nav1" class="navbar">
    <li><a href="#home">Home</a>
    </li>
    <li><a href="#news">Text one</a>
    </li>
    <li><a href="#news">Text Two</a>
    </li>
    <li class="dropdown">
      <a href="javascript:void(0)" class="dropbtn" onclick="myFunction()">Dropdown ▼</a>
      <div class="dropdown-content" id="myDropdown">
        <a href="#">Link 1</a>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </li>

  </ul>

  <div class="main">


    z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>z
    <br>

  </div>
</body>