Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top