【发布时间】:2014-05-29 11:54:10
【问题描述】:
使用 scrollto.js 脚本使用 next/prev 按钮从一个部分导航到另一个部分工作正常。 问题是导航滚动到浏览器的顶部。如何让“容器”从浏览器顶部偏移 100 像素。 (因为固定的“顶级容器”)
(在css中尝试了所有方法都不成功,相信答案在javascript中)
有没有办法在不修改 scrollto 脚本的情况下强制 div 从顶部偏移?
<!-- FIXED TOP CONTAINER 100px -->
<div id="fixed-top-container">
...
</div>
<!-- PREVIOUS / NEXT -->
<a id="prev" href="#">
<div id="float-previous">
</div>
</a> <a id="next" href="#">
<div id="float-next">
</div>
</a>
<!-- CONTAINER -->
<div id="container">
<!-- SECTION 1 -->
<div class="section current" id="section-1">Height:200px</div>
<!-- SECTION 2 -->
<div class="section" id="section-2">Height:400px</div>
<!-- SECTION 3 -->
<div class="section" id="section-3">Height:800px</div>
<!-- SECTION 4 -->
<div class="section" id="section-4">Height:900px</div>
<!-- SECTION 5 -->
<div class="section" id="section-5"><span class="style1">Height</span>:1000px</div>
</div>
<!-- SCRIPT -->
<script>
$(function() {
function scroll(direction) {
var scroll, i,
positions = [],
here = $(window).scrollTop(),
collection = $('.section');
collection.each(function() {
positions.push(parseInt($(this).offset()['top'],10));
});
for(i = 0; i < positions.length; i++) {
if (direction == 'next' && positions[i] > here) { scroll = collection.get(i); break; }
if (direction == 'prev' && i > 0 && positions[i] >= here) { scroll = collection.get(i-1); break; }
}
if (scroll) {
$.scrollTo(scroll, {
duration: 600
});
}
return false;
}
$("#next,#prev").click(function() {
return scroll($(this).attr('id'));
});
$(".scrolltoanchor").click(function() {
$.scrollTo($($(this).attr("href")), {
duration: 600
});
return false;
});
});
</script>
【问题讨论】:
-
你试过
#container { margin-top: 100px; }吗?你能用你的css做一个jsfiddle.net吗? -
是的,:(,它不起作用,滚动位置div的顶部到浏览器的顶部,...
-
为什么不使用固定位置?
-
使用固定位置防止滚动到部分。
-
javascript中有没有办法抵消特定的div?
标签: javascript