views:

291

answers:

2

What is scrollTop and scrollHeight in HTML elements.

I added a function

function fnCheckScroll(){   
    var iNewHeight = oDiv.scrollHeight;
    var iscrolTop = oDiv.scrollTop; 
    alert("The value of the scrollHeight property is: "  + iNewHeight + "px"); 
    alert("The value of the scrollTop property is: " + iscrolTop + "px");        
}

<div id="oDiv" style="overflow: scroll; height= 100px; width= 250px; text-align: left">
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
Hello<br>
</div>

I checked in ie8. Always

The value of the scrollTop property is: 0 - returns 0 for scrollTop (even after scrolling)?

+4  A: 

See the MDC articles, scrollTop and scrollHeight. In summary, scrollTop is how much it's currently scrolled, and scrollHeight is the total height, including content scrolled out of view.

Matthew Flaschen
How about the browser support of above properties, does all browser support same property? specifically in IE 8?
Lakshman
They were introduced by MS in IE4, and are still non-standard. All modern browsers should support them, but Opera apparently gives incorrect values for scrollHeight. See [Quirksmode](http://www.quirksmode.org/dom/w3c_cssom.html#t35).
Matthew Flaschen
Also, they are [being specified](http://www.w3.org/TR/cssom-view/#scroll-attributes) in CSSOM View Module, a W3C working draft.
Matthew Flaschen
+3  A: 

If I scroll down 5px in this window, the window's scrollTop value is 5. If I scroll right 10px in a scrollable div, the div's scrollLeft value is 10.

When I scroll to the top left corner of this window, both its scrollTop and scrollLeft values are 0.

Matchu
Does scrollTop property is supported by IE8?
Lakshman
@Lakshman: I'm pretty sure every modern browser, and even older browsers like IE6, support this method.
Matchu