You can use the scrollTop property of the HTMLElement property to set the amount by which its contents scroll.
And you can get the amount needed to scroll from the offsetTop of the element you want to scroll.
For example, using this HTML:
<div id="container"> <p id="item-1">foo</p> <p id="item-2">bar</p> <p id="item-3">baz</p> </div>
You can use this JavaScript to scroll the container div in the third paragraph:
document.getElementById("container").scrollTop = document.getElementById("item-3").offsetTop;
Reene saarsoo
source share