I have a problem with jquery, I made a fixed header but, when it scrolls down it's not aligned horizontally. In CSS, i put margin-left:auto;margin-right:auto; and it doing it right, but when i scroll down the header is going to the left side.
I don't know if is the problem in jquery or in CSS.
Thanks a lot.
Here's my jQuery:
$(function () {
var HeaderTop = $('#header').offset().top;
$(window).scroll(function () {
if ($(window).scrollTop() > HeaderTop) {
$('#header').css({
position: 'fixed',
top: '0px',
marginLeft: 'auto',
marginRight: 'auto'
});
} else {
$('#header').css({
position: 'relative',
top: '0px'
});
}
});
});
My CSS:
html, body
{
height:100%;
}
body
{
margin: 0;
padding: 0;
}
#header
{
width:900px;
height:100px;
background-color:#FFF;
margin-left:auto;
margin-right:auto;
border-top:1px;
border-top-color:#D2D2D2;
border-top-style:solid;
border-bottom:1px;
border-bottom-color:#D2D2D2;
border-bottom-style:solid;
}
header
{
background-color:#FFF;
width:900px;
margin-left:auto;
margin-right:auto;
text-align:center;
}
.content
{
width:900px;
margin-left:auto;
margin-right:auto;
}
#header ul
{
list-style:none;
}
#header ul li
{
display:inline;
padding:10px;
}
My HTML:
<header>
<img src="darkness.png" height="100" />
</header>
<div id="header">
<ul>
<li>Home</li><li>About</li>
</ul>
</div>
<div class="content">
</div>
jsFiddle Demo: http://jsfiddle.net/24ba7