我正在尝试垂直和绝对水平地修复标题。我正在使用以下代码。
<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>
正文标签
$(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;
}
我尝试在 div
中使用 class attrb 以及 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)
请帮忙。这非常重要。
最佳答案
您需要像这样在页面的头部包含 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());
});
});
关于javascript - 如何为除法垂直和绝对水平固定位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15887765/