我已经有一段时间了,我只是卡住了。我正在使用 Win XP、IE8 和 JQuery 1.4.4。
我想要的是导航菜单从标题下开始。但是一旦标题不在视图中,导航菜单将停留在滚动窗口的顶部。当用户向上滚动并且标题重新出现时,导航菜单应该再次停留在标题下方。
现在,它是浮动的,但没有调整我想要的方式。
这是我尝试过的代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Please make this scrolling work</title>
<link href="Styles/Navigation.css" rel="Stylesheet" type="text/css" />
<script src="Scripts/jquery-1.4.4.min.js" language="javascript" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$(window).scroll(function () {
//var bottom = $('#contents').height() + 100;
var bottom = $(window).height();
var top = $('#header').height();
var top_start = top + 1;
var top_limit;
if (document.body.scrollTop <= top_start) {
top_limit = top_start;
}
else {
top_limit = 0;
}
$('#menu-bar').css('top', (top_limit) + "px");
//$('#menu-bar').css('bottom', (bottom) + "px");
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="header" style="clear:both;height:40%;">
stuff
<br />
<br />
more stuff
<br />
</div>
<div class="menu-bar" style="float:left;width:25%;" id="menu-bar">
<ul>
<li><a href="#mainpage">Main Page</a>
<ul>
<li><a href="#search">Search</a></li>
<li><a href="#foo">Foo</a></li>
<li><a href="#bar">Bar</a></li>
<li><a href="#baz">Baz</a></li>
<li><a href="#blah">Blah</a></li>
</ul>
</li>
</ul>
</div>
<div id="contents" style="float:left;margin-left:25%;">
olwqfjapnwvqjpfqjwfeoqjfepqfewjqpfjnqpefpkqjmewpofqne
<br />
powjunvejpqlewkjrvqpnewkjmqwofvjnewpfoqewkjponqejpewfmqewp
<br />
</div>
以及菜单的 CSS:
.menu-bar{
position: fixed;
overflow:scroll;
height:100%;
/*top:0;
bottom:0;*/
}
编辑: 这是修复。
<script language="javascript" type="text/javascript">
$(window).scroll(function () {
//var bottom = $('#contents').height() + 100;
var bottom = $(window).height();
var top = $('#header').height();
var top_start = top + 1;
var top_limit;
if ($(window).scrollTop() <= top_start) {
top_limit = top_start;
}
else {
top_limit = 0;
}
$('#menu-bar').css('top', (top_limit) + "px");
$('#menu-bar').css('bottom', (bottom) + "px");
});
</script>
</div>
</form>
</body>
</html>
虽然我不太明白为什么:
top_limit = 0; //works but...
top_limit = $(window).scrollTop(); //doesn't work.