LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Menu Bar Stick To Top After Scrolling A Web Page Using Pure Vanilla JavaScript

Menu Bar Stick To Top After Scrolling A Web Page Using Pure Vanilla JavaScript

Menu-Bar-Stick-To-Top-After-Scrolling-A-Web-Page-Using-Pure-Vanilla-JavaScript

Have you ever seen one of those fixed (or “sticky’) header bars that disappear when you begin scrolling down the page, then reappear whenever you start to scroll back up? In this exercise, we’ll learn how to build our own using a bit of JavaScript.

Sticky navigation (navbar, header navigation) is a modern site navigation design concept to improve the navigation experience on your long web page. A sticky header navigation system will be always be fixed on the top no matter how your visitors scroll down or up the web page.

There are many code snippets available online or on many other blogs and website but everyone is not able to optimize your blog or website so you need some optimized code snippet. So now checkout out code snippet for your blog and website that will give you all features for your desired code. Now grab the ready to use code and paste it where you want.

Features:

  1. It Will Work On All Major Browsers.
  2. Pure Vanilla JavaScript Code.
  3. No External JQuery Or Script.
  4. Small Snippet Code With Easy Understanding.
  5. Toggle Function, One Page Scroll Stick Other Relative.

How To Show Menu Bar Stick To Top After Scrolling A Web Page Using Pure Vanilla JavaScript?

There are few easy and understandable steps to achieve your desired functionality using pure Vanilla JavaScript that we are gonna share below. Follow each step perfectly.

Recommended For You:
How To Add Non-Removable Credit Link In Template To Protect?

HTML:

<header class="header">This is a Header</header>
<div id="nav" class="nav">Main Navigation</div>
<div class="content">Hello World!</div>
<footer class="footer">This is a Footer</footer>

CSS:

* {margin:0;padding:0;}
.nav {
  border: 1px red dashed;
  background: #00ffff;
  text-align:center;
  padding: 21px 0;

  margin: 0 auto;
  z-index:10; 
  width:100%;
  left:0;
  right:0;
}

.header {
  text-align:center;
  padding: 65px 0;
  border: 1px red dashed;
}

.content {
  padding: 500px 0;
  text-align:center;
  border: 1px red dashed;
}
.footer {
  padding: 100px 0;
  text-align:center;
  background: #777;
  border: 1px red dashed;
}

JavaScript:

var startProductBarPos=-1;
window.onscroll=function(){
  var bar = document.getElementById('nav');
  if(startProductBarPos<0)startProductBarPos=findPosY(bar);

  if(pageYOffset>startProductBarPos){
    bar.style.position='fixed';
    bar.style.top=0;
  }else{
    bar.style.position='relative';
  }

};

function findPosY(obj) {
  var curtop = 0;
  if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    }
    curtop += obj.offsetTop;
  }
  else if (obj.y)
    curtop += obj.y;
  return curtop;
}

Final Words:

Be aware that the is placed well in your document. Rest all is in your hand if you want to customize it or play with it. That’s all we have. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allowed to us. Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

  

Be the first to write a comment.

Leave a Reply

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