最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

scroll - Setting HTML page vertical position with JavaScript - Stack Overflow

programmeradmin0浏览0评论

I have a HTML page that scrolls up and down (not a lot, but it does scroll). How can I set the scroll position in the page after executing some JavaScript?

I'm using jQuery to inject some additional HTML at the bottom of the page and I'd like to programmatically scroll to the position of that new content after it's added.

I have a HTML page that scrolls up and down (not a lot, but it does scroll). How can I set the scroll position in the page after executing some JavaScript?

I'm using jQuery to inject some additional HTML at the bottom of the page and I'd like to programmatically scroll to the position of that new content after it's added.

Share Improve this question edited May 27, 2011 at 9:08 Mateen Ulhaq 27.3k21 gold badges119 silver badges152 bronze badges asked Oct 16, 2008 at 19:10 rp.rp. 17.7k14 gold badges65 silver badges80 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Try using 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>

Another way to do this, so that you have the option:

In the HTML you are adding to the bottom of the page, you can insert a named anchor tag and then change the URL so that the page moves there (FYI: it will not refresh the page).

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

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

Depending on what you are doing, this may or may not be a useful solution.

发布评论

评论列表(0)

  1. 暂无评论