我有jQuery的問題固定的頭,我做了一個固定的頭,但,當它向下滾動它不是水平對齊。在CSS中,我把margin-left:auto; margin-right:auto;它做對了,但是當我向下滾動標題是去左側。CSS,jQuery的水平對齊
我不知道,如果是的jQuery或CSS的問題。
非常感謝。
這裏是我的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'
});
}
});
});
我的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;
}
我的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演示:http://jsfiddle.net/24ba7
這裏是代碼的的jsfiddle:http://jsfiddle.net/24ba7/ – Johannes