Scroll to Top Position of DIV using JavaScript

Thursday, May 1, 2008

Most of the JavaScript developers wonder why "Anchor List" not work in DIV's, it works fine in Mozilla Firefox but not in Internet Explorer.

When I was working on one of my personal projects, at one instance I need to implement the "Anchor List" functionality in DIV's, I have added "Anchor List" and placed the same in a DIV and that DIV has got constant height, if content is more than 400px then it will display a vertical scroll bar towards DIV's right. When user clicks on the Anchor List it used move to the desired list and this works fine in Mozilla but not in Internet Explorer. I have tried couple of scenarios and find out one solution which works fine in both the browsers that is Mozilla and Internet Explorer.

Scroll Position Of DIV

<script language="javascript">
function scrollToTop()
//document.getElementById("content").scrollTop = strPos; //Enter your desired Position
document.getElementById("content").scrollTop = 0; //It scrolls to top

<a href="javascript:void(0)" onclick="scrollToTop()">Scroll To Top </a>

Download the Example from here: ScrollPosition of DIV