2013-04-08 54 views
0

我试图垂直和绝对水平地修复标题。我正在使用以下代码。如何为一个分区垂直和绝对水平地固定位置

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<link href="style.css" rel="stylesheet" type="text/css"> 
<script src="header_position.js" type="text/javascript"></script> 
</head> 

body标签

$(window).scroll(function(event) { 
$("#headerpos").css("margin-left", 0-$(document).scrollLeft()); 
}); 



#headerpos{ 
position:fixed; 
border-top:8px solid #8DC540; 
background:#1E415B; 
width:956px; 
padding-bottom:7px; 
margin:auto; 
z-index:100; 
} 

我尝试使用类attrb在div以及ID attrb

.header{ 
position:fixed; 
border-top:8px solid #8DC540; 
background:#1E415B; 
width:956px; 
padding-bottom:7px; 
margin:auto; 
z-index:100; 
} 

而且通过去除margin:auto尝试;

我看到了这一切从链接Position a Div "Fixed" Vertically and "Absolute" Horizontally within a "Position:Relative" Container Div

但它无法正常工作,并在Chrome中,如果我去检查元素我看到一个错误:

Uncaught Reference error: $ is not defined (at line 1)

请帮助。这很重要。

回答

0

您需要包括jQuery的在你的页面这样的头:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script src="header_position.js" type="text/javascript"></script> 

此外,请务必将您的jQuery的文件准备好电话:

$(document).ready(function() { 
    $(window).scroll(function (event) { 
     $("#headerpos").css("margin-left", 0 - $(document).scrollLeft()); 
    }); 
}); 
+0

现在没有得到$未定义的错误,但向左滚动到右侧不工作它修复。滚动左下方的数据正在滚动,而不是标题。 – user2259065 2013-04-09 13:32:00

0

如果$不定义,你需要jQuery。只包括

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>之前的其他脚本。

我还建议将两个script标签放在页面底部以使其加载速度更快。

+0

现在没有收到$未定义的错误,但向左滚动右键不能修复它。滚动左下方的数据正在滚动,而不是标题。 – user2259065 2013-04-09 13:31:29

0

如果您在css中使用固定位置,则不需要js水平地为div定位div。 试试这个。

.header{ 
    position:fixed; 
    border-top:8px solid #8DC540; 
    background:#1E415B; 
    width:956px; 
    padding-bottom:7px; 
    margin-left:-478px; 
    left:50%; 
    z-index:100; 
} 
相关问题