A navigation menu that stays on top with jQuery
This is an easy way to make a navigation menu that sticks to top of the window while scrolling down the page. We want the menu to stick to the top only after we've scrolled down further than its original position. A few lines of jQuery will to the trick. Here is the markup:
HTML
<div id="demo_top_wrapper">
<!-- a header with a logo just to have some content before the menu -->
<div id="demo_top">
<div class="demo_container">
<div id="my_logo">Our logo</div>
</div>
</div>
<!-- this will be our navigation menu -->
<div id="sticky_navigation_wrapper">
<div id="sticky_navigation">
<div class="demo_container">
<ul>
<li><a href="#" class="selected">HOME</a></li>
<li><a href="#">ABOUT US</a></li>
<li><a href="#">SERVICES</a></li>
<li><a href="#">CLIENTS</a></li>
<li><a href="#">PARTNERS</a></li>
<li><a href="#">CONTACT</a></li>
</ul>
</div>
</div>
</div>
</div><!-- #demo_top_wrapper -->
<!-- some other content should go here, in order to have a scrollbar -->
CSS
.demo_container { width:980px; margin:0 auto; }
#demo_top_wrapper { margin:0 0 20px 0; }
#demo_top { height:100px; padding:20px 0 0 0; }
#my_logo { font:70px Georgia, serif; }
/* our menu styles */
#sticky_navigation_wrapper { width:100%; height:50px; }
#sticky_navigation { width:100%; height:50px; background:url(trans-black-60.png); -moz-box-shadow: 0 0 5px #999; -webkit-box-shadow: 0 0 5px #999; box-shadow: 0 0 5px #999; }
#sticky_navigation ul { list-style:none; margin:0; padding:5px; }
#sticky_navigation ul li { margin:0; padding:0; display:inline; }
#sticky_navigation ul li a { display:block; float:left; margin:0 0 0 5px; padding:0 20px; height:40px; line-height:40px; font-size:14px; font-family:Arial, serif; font-weight:bold; color:#ddd; background:#333; -moz-border-radius:3px; -webkit-border-radius:3px; border-radius:3px; }
#sticky_navigation ul li a:hover, #sticky_navigation ul li a.selected { color:#fff; background:#111; }
jQuery
$(function() {
// grab the initial top offset of the navigation
var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
// our function that decides weather the navigation bar should have "fixed" css position or not.
var sticky_navigation = function(){
var scroll_top = $(window).scrollTop(); // our current vertical position from the top
// if we've scrolled more than the navigation, change its position to fixed to stick to top,
// otherwise change it back to relative
if (scroll_top > sticky_navigation_offset_top) {
$('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
} else {
$('#sticky_navigation').css({ 'position': 'relative' });
}
};
// run our function on load
sticky_navigation();
// and run it again every time you scroll
$(window).scroll(function() {
sticky_navigation();
});
});





Alberto • March 2, 2012 07:28
I just have one question:
Playing with the demo i increased the height of the id demo-top, to 1000px because in my site i have a navigation bar at that position. but as soon as i scrolled the demo the nav became fixed and appear on top.
i would wish to know how to code it so, the navigation become fixed once i have scrolled till 1000px in my site and from there follow my scrolling
it is possible with this code?
thanks in advance
Jai • March 22, 2012 13:18
Simo • March 30, 2012 05:38
I can get this to work offline if i use jquery 1.6.4 as in the examples, but i'm coding this into a wordpress site and apparently changing the jquery version isn't the best idea for security's sake.
Any chance you can update the code or show me what i need to change so i can get this working with jquery 1.7.1 or above?
Thanks very much!
~Simo
brion • April 4, 2012 22:26
ive been looking all day for a simple way to get the nav visible at all times and this was the simplest.
thanks so much!
bri
patrioticcow • April 5, 2012 22:57
Simo • April 10, 2012 13:27
http://mhuntdesign.com/blog/jquery/change-default-jquery-version-wordpress/
Hope this helps someone else out there.
Gabie • April 13, 2012 11:05
Someone please help :D
Masum • April 19, 2012 13:25
Masoud • May 16, 2012 18:19
Thank you for this tutorial
Nikos Tsaganos • June 4, 2012 12:44
Regarding some issues mentioned above, I couldn't reproduce them. I tried the demo with the latest jQuery 1.7.2 and it works. It also worked normally for me under Firefox/IE/Chrome/Safari with a header of 1000px. If you still have trouble with this, please share a link to see if I'm able to help :)
Felix Stekolshchik • June 17, 2012 02:03
Ok so i'm trying to center the nav on the demo, and was able to reproduce a centered nav from a site a did a while back, but when i scroll up, it shifts everything to the left. Is there a way to do it so everything just stays centered?
safvanPP • July 1, 2012 01:24
keep it up
best of luck
giorgos k • July 5, 2012 11:05
$('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
} else {
$('#sticky_navigation').css({ 'position': 'relative' });
}
Lets say I want a new div to appear when position is relative. Is there a better way than:
if (scroll_top > sticky_navigation_offset_top) {
$('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
$('#sticky_divtoappear').css({ 'display': 'none' });
} else {
$('#sticky_navigation').css({ 'position': 'relative' });
$('#sticky_divtoappear').css({ 'display': 'block' });
}
matthew f • July 19, 2012 14:36
it works just great!
I only have one question: how can I change the menu alignment to center or right??
it seems I can't get around this issue...
thanks in advance
M
Chunny • August 9, 2012 20:45
Oliver • August 10, 2012 12:20
, 'left':0
Natarajan • August 12, 2012 23:13
zinzinzibidi • September 10, 2012 11:03
Angel • September 10, 2012 19:45
Otter Creative Studio • September 30, 2012 00:42
Gopikrishna S • October 2, 2012 17:54
Satinder Singh • October 4, 2012 12:01
raman • October 9, 2012 13:59
raman • October 9, 2012 14:00
sanjeev • October 12, 2012 13:56
milky • October 20, 2012 05:02
I am a noob in jquery so any help would be appreciated... Simo, maybe you could enlighten me on your integration process...
thanks
NickHarty • October 23, 2012 00:06
Solution:
Be sure to specify the HEIGHT of the image/div/whatever ABOVE the navigation bar. This was my problem. I hadn't specified the height of the image above the navigation bar and so the jQuery assumed the image wasn't there.
Thank you!
Icone • October 29, 2012 07:05
Harikrishna • November 4, 2012 10:21
Thanks again :)
Harikrishna
Vaidas • November 9, 2012 16:29
can anyone just simply say where should be copied jQuery part of code if using the WordPress?
Functions.php?
Thanks in advance..!
David Roberson • November 16, 2012 11:03
Anyone run into this? If so, anyone know how to fix it?
David Roberson • November 17, 2012 02:29
Jguiss • November 19, 2012 11:10
Lauren • November 19, 2012 21:29
Murat • November 23, 2012 01:27
taylor • November 27, 2012 22:59
$(window).scroll(function () {
if ($(this).scrollTop() > 148) {
$('.sub-header-wrap').css({ 'position': 'fixed', 'top':0, 'left':0 });
} else {
$('.sub-header-wrap').css({ 'position': 'absolute', 'top':148 });
}
});
ฟังเพลงออนไลน์ • November 28, 2012 07:16
LosDobos • December 1, 2012 00:46
Now create a folder called "js" in your child them folder (where functions.php is). Inside that new folder create a file called "custom_script.js" and paste the jQuery into it.
In theory that should work...
LosDobos • December 2, 2012 02:55
start php tag goes here
if ( !is_admin() ) { // instruction to only load if it is not the admin area
// register your script location, dependencies and version
wp_register_script('custom_script',
get_bloginfo('stylesheet_directory') . '/js/custom_script.js',
array('jquery'),
'1.0' );
// enqueue the script
wp_enqueue_script('custom_script');
}
end php taggoes here
L. Mercury • December 8, 2012 07:35
I have been banging my head for the past three hours with the "stickyfloat" script. This was the perfect, most painless solution.
Thank you so, so much.
Julia • December 14, 2012 17:48
Thanks, this is a wonderful demo!
Odnel • December 15, 2012 23:01
Justin • January 1, 2013 04:49
if (scroll_top > sticky_navigation_offset_top) {
$('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
$('#sticky_navigation').css("visibility", "visible");
$('#sticky_navigation').css("display", "block");
} else {
$('#sticky_navigation').css({ 'position': 'relative' });
$('#sticky_navigation').css("visibility", "hidden");
$('#sticky_navigation').css("display", "none");
}
pipicom • January 2, 2013 17:01
laura • January 19, 2013 05:12
thanks!!
laura • January 19, 2013 05:58
Saliem • February 3, 2013 05:24
Sanjay • February 7, 2013 05:58
Ramsay • February 17, 2013 19:12
Allison • February 24, 2013 02:51
Rohan Shewale • February 24, 2013 14:13
Thanks for the help..!
naresh • February 26, 2013 23:05
csslove • February 28, 2013 17:28
shanu • March 2, 2013 03:43
Nuzree • March 23, 2013 06:48
Have a nice day.
All the best. !
Tobias Redmann • March 25, 2013 16:21
KPCTA • April 8, 2013 00:31
Thank you in advance.
Webdesign Soest • April 8, 2013 15:32
quangpd • April 12, 2013 08:02
It helpful with me :)
Babaroga • April 19, 2013 23:37
Roselle • April 21, 2013 10:57
I have 2 questions:
1) how can I make whatever I've positioned to always stay on top? some pictures and facebook buttons show up over whatever is fixed;
and
2) how can I change the css of whatever's fixed? for example, my main nav is fixed, when it moves down, I want to change the background color. Is this possible?
Thanks a bunch!
Roselle
Oscar • April 29, 2013 22:01
Under what line?
Your help is much appreciated.
Regards,
--------------------------------------------------------------------------------------------------------------
patrioticcow • April 5, 2012 22:57
nice. some people ,mighty want to add 'z-index': 100000 so the menu will stay on top of any image sliders or whatever.