I have implemented the JavaScript code below to set the header to a fixed position when it reaches the top of the page so that it remains visible while the user scrolls. Everything appears to be functional, but the header movement is abrupt and not smooth. What am I overlooking? You can test it live here:
$(function(){
// Check the initial Position of the Sticky Header
var stickyHeaderTop = $('#headerWrapper').offset().top;
$(window).scroll(function(){
if( $(window).scrollTop() > stickyHeaderTop ) {
$('#headerWrapper').css({position: 'fixed', top: '0px'});
} else {
$('#headerWrapper').css({position: 'static', top: '0px'});
}
});
});
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Martin Izehi</title>
<meta name="description" content="" />
<meta name="keywords" content="">
<meta name="viewport" id="viewport" content="width=device-width, minimum-scale=1.0, user-scalable=0, maximum-scale=10.0, initial-scale=1.0" />
<link rel="shortcut icon" href="assets/elements/favicon.ico">
<link rel="stylesheet" type="text/css" href="assets/css/reset.css">
<link rel="stylesheet" type="text/css" href="assets/css/core.css">
...
CSS:
body {
background: #000000 url('../elements/background.png') no-repeat center top;
line-height: 125%;
text-align: center;
overflow-x: hidden;
...