Sticky Header on Scroll

Sticky Header on Scroll with CSS and Jquery

by Glynne Turner

HTML

<div id="main">
<div id="header">
	<h1><a href="#">Home</a></h1>
</div>
<div id="navbar">
<ul>
		<li><a href="#">Menu 1</a></li>
		<li><a href="#">Menu 2</a></li>
		<li><a href="#">Menu 3</a></li>
		<li><a href="#">Menu 4</a></li>
</ul>
</div>
</div>

CSS

/*---------------------------------------------------
	RESET
----------------------------------------------------*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
	margin: 0;
	padding: 0;
	border: 0;
	font-size: 100%;
	font: inherit;
	vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
	display: block;
}
body {
	line-height: 1;
}
ol, ul {
	list-style: none;
}
blockquote, q {
	quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
	content: '';
	content: none;
}
table {
	border-collapse: collapse;
	border-spacing: 0;
}


body{
	background:#DDD;
}
#main{
	min-height:1000px;
}
#navbar{
  width:100%;
  height:45px;
  line-height:45px;
  background:#FFF;
	margin:0 auto;
}
#navbar a{
	text-decoration:none;
	color:#5AA628;
}
#navbar ul li{
  list-style:none;
  float:left;
  display:block;
  padding:0 15px;
}
#header{
	width:100%;
	line-height:35px;
	display:block;
	background:#01ACE2;
	margin:0 auto;
  height:150px
}
#header a{
	font-family:Arial, Helvetica, Serief;
	color:#FFF;
	font-size:14px;
	padding:0 15px;
	text-decoration:none;
}
.fixed#navbar{
	width:100%;
	position:fixed;
	top:0;
	left:0;
	background:#FFF; 
	border-top:4px solid #01ACE2;
	box-shadow:0px 1px 5px #999;
		transition:all 0.7s ease 0s;
	-webkit-transition:all 0.7s ease 0s;
	z-index:100;
}
.fixed#navbar a{
	color:#01ACE2;
	font-size:24px;
}

JavaScript

jQuery(document).ready(function() {
	$(window).scroll(function() {
		var sticky = $('#navbar'),
			scroll = $(sticky).scrollTop();
		if (scroll > 0) sticky.addClass('fixed');
		else sticky.removeClass('fixed');
	});
});