I'm creating a header that'll stick in place when the user scrolls down past a set amount of pixels. My header is 121 pixels high, but the part of the header I want to stick is the bottom 42 pixels of the header; which does work, but at the point it begins to stick the page jumps up about 50 pixels.
My thought on what could be causing the issue is $('#header').css({position: 'fixed'});
. I think once fixed
is applied the content, the content div
no longer takes the margin into account. I've tried to play around with the different positions, but nothing has worked out for me.
I created a JSFIDDLE to better illustrate my issue.
JQUERY CODE
$(window).scroll(function()
{
if( $(window).scrollTop() > 79 )
{
$('#header').css({position: 'fixed'});
}
else
{
$('#header').css({position: 'static', top: '-79px'});
}
});
CSS CODE
body {
margin: 0;
padding: 0;
}
#header {
width: 100%;
height: 121px;
background: url(http://libertyeaglearms.com/dev/admin/graphics/header-bg.png);
}
#content {
margin: 10px auto;
width: 300px;
min-height: 600px;
}
HTML
<div id="header">header</div>
<div id="content">content goes here</div>