【发布时间】:2014-06-02 09:03:02
【问题描述】:
当我的内容位于指定了 overflow-y: scroll 的固定位置容器内时,我似乎无法让 scrolltop 工作。
这是我的相关代码:
/* some container with many large content blocks inside */
#container {
position: fixed;
width: 300px;
height: 300px;
overflow-y: scroll;
}
/* button that has a data-path attribute that specifies where the container should scroll to*/
$(".button").on("click", function(){
var path = $(this).attr("data-path");
var anchor = $("#" + path);
var position = anchor.position().top;
$("#container").animate({scrollTop: position});
});
我相信这个小提琴很好地说明了我的困境:http://jsfiddle.net/Qndu5/
如果你从上向下滚动到一个元素,效果很好。在那之后,所有的赌注都被取消了。它完全无法从顶部以外的任何位置滚动。它要么严重错过了标记,要么一直滚动到顶部,即使提供给它的位置值似乎是正确的。
我当然在这里遗漏了一些东西,但我不确定我不理解什么。感谢您提供的任何帮助!
【问题讨论】:
标签: javascript jquery html css css-position