Link to home
Start Free TrialLog in
Avatar of mmuruganandam
mmuruganandamFlag for United States of America

asked on

How to findout the ScrollBar Position using JavaScript?

within the <body> tag, a <div id="content"> ... rest of the body contents goes here ... </div></body>

Upon rendering the page, i have the scroll bars (vertical) enabled.  I can able to scroll it.
but the JS calls, document.body.scrollLeft and document.body.scrollTop are always giving 0 (Zero).

I am testing this with IE.  It works fine in FireFox.
What is the exact problem in this with IE?


Thanks,
Muruga
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

This works for me:

<html>
<head>
<script>
function moveMenu() {
    for(i = 1 ; i < 5; i++) {
      mDiv = document.getElementById('div_menu_links_'+i);
         if(mDiv)
         {
              mDiv.style.top = (i * 21) + document.body.scrollTop;
              window.status = "top: "+mDiv.style.top;
         }
    }
}
</script>
</head>
<body onScroll="moveMenu()">
<div name="menu_links_1" id="div_menu_links_1" style="top=21; left:0; z-index:106; background-color:yellow; position:absolute;">
<table>
<tr><td>MenuA</td>
<td>MenuB</td>
<td>MenuC</td></tr>
</table>
</div>
<table height="1600" border="1">
<tr><td></td></tr>
</table>
</body>
</html>


Avatar of mmuruganandam

ASKER

>>document.body.scrollTop
Is there any other way than this.

Because, this is not working for me.  We have some intranet standards.  
All the stylesheets are defined even for DIV.  

Can you give me a different solution?

Thanks
Avatar of migoEX
migoEX

Maybe you scrollbar is inside the DIV object, and not the BODY. You can try to retrieve the "scrollTop" property of the DIV element itself.
I have found out the solution for this.
It can be achieved by document.documentElement.scrollTop.  
It is working fine.

Thanks for your support as always.  
I am confused whether to delete this question or give away points.
Any suggestions guys....
ASKER CERTIFIED SOLUTION
Avatar of Cem Türk
Cem Türk
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial