Sticky Navbar Using Css And Javascript

Sticky Navbar Using Css And Javascript
Sticky Navbar Using Css And Javascript
Sticky Navbar Using Css And Javascript

In Many Websites we saw that when we scroll page in bottom then the header stop on top and all content goes up.

In This article we will learn How to Creat Sticky Navbar Using Css And Javascript

HTML

<div class="header">
  <div class="container">
   <div class="logo"><img src="my_logo.jpg" /></div>
   <div class="wright_menu">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About Us</a></li>
        <li><a href="#">Services</a></li>
        <li><a href="#">Feedback</a></li>
        <li><a href="#">Contact Us</a></li>
      </ul>
   </div>
  <div class="clear"></div>
  </div>
</div>
<br /><br />

<div class="content">
   <div class="container bg_white pd-15">
       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.<br />
   </div>
</div>

CSS

body
{
	font-family:Verdana, Geneva, sans-serif;
	font-size:13px;
	margin:0px;
	padding:0px;
	background:#f2f2f2;
}

h1,h2,h3,h4,h5,h6,p,ul,li
{
	margin:0px;
	padding:0px;
}

.fixed-header {
    position: fixed;
    top: 0;
	background:rgba(0,0,0,1);
    left: 0;
    width: 100%; 
}


.clear
{
	clear:both;
}

.header
{
	width:100%;
	height:95px;
	background:#ffffff;
}

.container
{
	width:80%;
	height:auto;
	margin-left:auto;
	margin-right:auto;
}

.logo { width:auto; float:left; padding-top:15px;}
.wright_menu{ float:right;}
.wright_menu ul {list-style:none;}
.wright_menu ul li { float:left; padding:10px; margin-top:30px; font-size:16px; text-decoration:none;}
.content{height:1000px;}
.bg_white{ background:#fff;}
.pd-15{ padding:15px; font-size:18px;}

JavaScript

$(window).scroll(function(){
     if ($(window).scrollTop() >= 300) {
         $('.header').addClass('fixed-header');
     }
     else {
         $('.header').removeClass('fixed-header');
     }
 });

Download Source Code

Leave a Reply

Your email address will not be published. Required fields are marked *