• About Us
  • Our Team
  • Write For Us
  • Advertise
  • Contact Us
  • Internet Marketing
    • Social Media
    • SEO
    • Blogging
    • Content Marketing
  • Programming Talk
    • Code Snippets
    • Jquery
    • PHP
    • Web Design
    • WordPress
    • Spider
  • Technology
    • Mobile Phones
    • Computer Softwares
    • Computer Hardware
      • Graphic Card
      • Motherboards
      • Networking
      • Power Supply
      • Storage


JUST IN

All Computer And Gadget Related Guides Compiled In One Place

New To Blogging? Familiarize Yourself With WordPress And How To Install It On Fatcow

Google Will Soon Launch The Google Web Designer Tool

Mobile Website Development: Planning

Looking To Buy A Laptop? Read These Guidelines First

Resident Evil: Revelations: The Story Unearthed

How Can Social Networking Help To Raise Your Domain’s Search Rankings

Want The Perfect Name For Your Domain? Here Are 3 Key Points To Help You

Beginner’s Guide Of Starting Your Own Blog

AMD To Release 8000 Series In Q3 2013


How To Create Sticky Navigation Bar


 November 21st, 2012   Posted by Umair Abid   13 Comments
Posted In: Jquery,Programming Talk,Web Design

jquery_banner

In last week, one of our clients asked us to develop a sticky navigation bar for his website. Such navigation or menu bars are pretty much used by every website. You may have noticed the menu bar which sticks to the top of the webpage or move along the page while you scroll down. Making a sticky header or any container which is already on the top of the page is not a big deal. All you need to do is apply fixed position to that container, it will become sticky and always remains on top, but situation becomes tricky if navigation bar is below header and you want to make it sticky after placing it on the top after when user scrolls down the page and header disappears. The Image below will help you to understand the task.

Above image is clearly defining our goal which is in short to place the navigation bar on top and make it sticky after user scrolls down and header disappears. For demo lets prepare a dummy html web page which will have the header, below header there will be a navigation bar and then there will be a content container with dummy content and a fixed height to create a scroll on the page. Here is the HTML and CSS or our dummy webpage.

<div class="header"><strong>Header</strong></div>
<div class="nav"><strong>Navigation Bar</strong></div>
<div class="content"></div>
<style>
body{
	margin: 0;
}
.header{
	height: 50px;
	background-color: #000;
	font-size: 16px;
	text-align: center;
	color: #fff;
	padding-top: 3%;
}
.nav{
	height: 30px;
	background-color: #D7D7D7;
	font-size: 16px;
	text-align: center;
	color: #000;
	padding-top: 5px;
	width: 100%;
}
.content{
	height: 1200px;
}
</style>

I guess the HTML and CSS is pretty straight forward, self explanatory and very easy to understand. Now, lets start thinking about the solution of our problem. Obviously we are going to use jquery to get our desired result. As you might have figured out that we will target the scroll event of DOM (Document Object Model) to trigger our jquery code for making the navigation bar sticky. As, we need to make the navigation bar sticky when user scrolls down and the header container will disappear so we also need to measure the scroll height. Okay, now lets start coding the action part which is coding the jquery.

<script type="text/javascript">
$(document).ready(function (){
	$(window).scroll(function() {
	}
}
</script>

The first line of code might seem very familiar if you have used jquery before, in this code you are selecting the whole document and then you will apply firing a function on ready event which will let you to execute your jquery when the webpage or document is completely loaded. In the second line we are again selecting the window and firing function on its scroll events. That means whatever we write between this function will be fired on the scroll event. Now, before proceeding forward we need to determine the scroll length from top when header disappears from the screen. We can either find it by the jquery function scrollTop() which will also be our key option or by measuring the length of our header container. You can also refer to Measuring user scroll with jquery.

In our case the value of scroll top is 90. So what we are going to do is to check the length of user top scroll if the length of user scroll  becomes greater or equal to 90 we will apply CSS to place the navigation bar on top and make it sticky. We can also use jQuery toggleClass function to change the CSS of navigation bar but I am comfortable doing it with jquery.

$(document).ready(function (){
	$(window).scroll(function() {
		var scrollTop = 90;
		if($(window).scrollTop() &gt;= scrollTop){
			$('.nav').css({
				position : 'fixed',
				top : '0'
			});
		}
	}
}

Once you have added the above code, the moment you will refresh the page, and scroll down your page as soon as the header will disappear from the screen navigation?? bar will be placed on top and it will move along the page, but if you scroll back on top you will notice that the navigation bar will remain on top of the page above header and we obviously don’t want to mess with the user experience. So we will apply another check on top scroll of the DOM and when it will become smaller than 90 we will simply remove our additional CSS. Here is final shape of our jquery code.

<script type="text/javascript">
$(document).ready(function(){
	$(window).scroll(function(){
		var scrollTop = 90;
		if($(window).scrollTop() >= scrollTop){
			$('.nav').css({
				position : 'fixed',
				top : '0'
			});
		}
		if($(window).scrollTop() < scrollTop){
			$('.nav').removeAttr('style');	
		}
	})
})</script>

Do give us Feedback on this by leaving comments below and if you want us to help you with anything or if you have suggestions. Let us know and we’ll be more than happy to assist. Enjoy and keep coding!

View Demo


Tags:  css, fixed navigatio bar, jquery, scroll top, sticky navigation bar, web design

About The Author

Umair Abid
Umair Abid is a Web Developer with expertise in PHP Frameworks(Zend, Yii), Wordpress and client side coding (HTML, CSS, jQuery, AJAX). Loves Linux, likes windows, hates MAC. He is a passionate programmer with keen interest to learn, share and explore. Follow him on twitter @umairabid_ or you can also add him on skype to discuss anything you want. His skype id is 'umairabidseo'


13 Responses to “How To Create Sticky Navigation Bar”

  1. Dan Allen says:
    January 25, 2013 at 10:45 am

    Hi Umair,

    great article and one of the most simple sticky navs I have seen. I was looking to use this on the menu that has a dropdown megamenu. When i use this code the dropdown function on the menu does not work . Is that common with these type of navigation menus?

    Regards,
    Dan

  2. Umair Abid says:
    January 25, 2013 at 12:37 pm

    Thanks Dan,
    Everything in the nav div should be moving along but its hard to figure out the problem without having look at the code. :)

  3. Naveen says:
    February 14, 2013 at 1:01 pm

    Hi,

    The code works great in IE and Chrome, the dumb IE makes it break the layout.

    I’m using a dropdown menu in it, but while scrolling in IE the menu moves ti right. I believe there is a z-index problem which I am not able to resolve.

  4. Naveen says:
    February 14, 2013 at 1:02 pm

    I mean firefox not IE .. it dosent work in IE

  5. Umair Abid says:
    February 14, 2013 at 2:42 pm

    Can you show me your code Naveen?

  6. Leroy says:
    April 8, 2013 at 6:04 pm

    Wow. Nice and simple. Definately using this..

  7. Surjith SM says:
    April 24, 2013 at 6:14 pm

    here is an interesting approach to Sticky navigation, Only shows navigation when user really wants to be.. http://web3canvas.com/item/sensitive-sticky-navigation-with-jquery/

  8. Umair Abid says:
    April 25, 2013 at 2:04 pm

    Good tutorial but I guess you only show the navigation bar when user scrolls up, what if? if user scrolls down and look for the nav bar?

  9. James says:
    May 19, 2013 at 12:55 am

    Great just what I was looking for I will bookmark this page for when I finish the design stage.

  10. Dave says:
    May 29, 2013 at 3:30 am

    Can this be used for custom menus on wordpress themes?
    Thnx

  11. Umair Abid says:
    May 29, 2013 at 11:29 am

    Yes you can

  12. Jeff says:
    June 7, 2013 at 6:15 pm

    how would I do this with a top that is dynamic (ex: not always 90)

  13. Umair Abid says:
    June 7, 2013 at 6:18 pm

    Place your dynamic content in the navigation container

Leave a Reply

Click here to cancel reply.

CommentLuv badgeShow more posts


  • Related Posts

    • mobilewebsitedevelopment Mobile Website Development: Planning
      June 10th, 2013
    • linkshare How to Parse LinkShare XML Coupon Codes Feed
      December 27th, 2012
    • php-jquery How to Create News Feed like Facebook with PHP and jQuery
      January 22nd, 2013
    • Example of Responsive Design 5 Benefits of Responsive Web Design (RWD)
      October 31st, 2012
  • Subscribe To Feeds

    Enter your email address:

  • Popular Posts

    • php-jquery How to Create News Feed like Facebook with PHP and jQuery
      January 22nd, 2013
    • jquery_banner How To Create Sticky Navigation Bar
      November 21st, 2012
    • Start-ups Post-launch etiquettes – 5 mistakes most startups make after launch
      October 24th, 2012
    • url What is Affiliate Marketing?
      December 11th, 2012
  • Categories

    • Affiliate Marketing
    • Affiliate Marketing Networks
    • Bing
    • Blogging
    • Code Snippets
    • Computer Hardware
    • Computer Softwares
    • Content Marketing
    • Facebook
    • Gaming Zone
    • Google AdSense
    • Graphic Card
    • Hard Drive
    • Inbound Marketing
    • Internet Marketing
    • Jquery
    • LinkedIn
    • Memory
    • Microsoft
    • Mobile Apps & Reviews
    • Mobile Phones
    • Motherboards
    • Networking
    • NEWS
    • PHP
    • Power Supply
    • Processors
    • Programming Talk
    • SEO
    • Social Media
    • Spider
    • Storage
    • Tablets
    • Technology
    • Twitter
    • Web Design
    • Wordpress
    • Yahoo
  • Ads – We got Bills

  • Recent Posts

    • techn All Computer And Gadget Related Guides Compiled In One Place
      June 18th, 2013
    • wordpress1 New To Blogging? Familiarize Yourself With WordPress And How To Install It On Fatcow
      June 15th, 2013
    • Google Google Will Soon Launch The Google Web Designer Tool
      June 11th, 2013
    • mobilewebsitedevelopment Mobile Website Development: Planning
      June 10th, 2013

  • RSS RSS

    • All Computer And Gadget Related Guides Compiled In One Place
    • New To Blogging? Familiarize Yourself With WordPress And How To Install It On Fatcow
    • Google Will Soon Launch The Google Web Designer Tool
    • Mobile Website Development: Planning
    • Looking To Buy A Laptop? Read These Guidelines First
  • Recent Posts

    • techn All Computer And Gadget Related Guides Compiled In One Place
      June 18th, 2013
    • wordpress1 New To Blogging? Familiarize Yourself With WordPress And How To Install It On Fatcow
      June 15th, 2013
    • Google Google Will Soon Launch The Google Web Designer Tool
      June 11th, 2013
  • Recenet Comments

    • Umair Abid on How to Create News Feed like Facebook with PHP and jQuery
    • Deb on How to Create News Feed like Facebook with PHP and jQuery
    • Nick on How to Parse LinkShare XML Coupon Codes Feed
    • Omar Sohail on Improve Your WiFi Performance By Following These Easy Steps
    • Tracy Baklund on Improve Your WiFi Performance By Following These Easy Steps
  • Search Blog


Copyright 2012 Techknowlogists