document.body.scrollTop or document.documentElement.scrollTop

Explain the function of static keyword and final keyword in Java in detail>>>

When using JavaScript to get the absolute coordinates of DOM node relative to the page, we need to calculate the scrolling distance of the current page, and the acquisition of this value depends on the browser

You can view the document.body corresponding to & lt; body>& lt;/ body> Document.documentelement is equivalent to the whole HTML, which indicates that browsers have different positions after rendering. FF, opera and IE think that the content of the page displayed in the client browser corresponds to the whole HTML, so document.documentelement is used to represent, The corresponding scrolling distance is obtained by document.documentelement.scrollleft and document.documentelement.scrolltop, while Safari and chrome browsers think that the page starts from the body part, so the corresponding scrolling distance is obtained by document.body.scrollleft and document.body.scrolltop. In addition, it should be noted that in the quirks mode of FF and ie, document. Body is used

For cross browser solutions, you can simply use the following code:

  

var

scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft);

var

scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);

Similar Posts: