使用JavaScript设置HTML页面的垂直位置

4

我有一个HTML页面,可以上下滚动(不算太多,但确实会滚动)。在执行一些JavaScript之后,如何设置页面的滚动位置?

我正在使用jQuery在页面底部注入一些额外的HTML,并希望在添加新内容后以编程方式滚动到该新内容的位置。

2个回答

5
尝试使用 window.scroll

Example:

// put the 100th vertical pixel at the top of the window
<button onClick="scroll(0, 100);">click to scroll down 100 pixels</button>

0

另一种方法是这样做,以便您有选择:

在您添加到页面底部的HTML中,您可以插入一个命名锚标签,然后更改URL,使页面移动到那里(FYI:它不会刷新页面)。

// add HTML like this, dynamically:
// <a name="moveHere" />

// the javascript to make the page go to that location:
window.location.hash = "moveHere";

根据你所做的事情,这可能是一个有用的解决方案,也可能不是。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接