en.javascript.info/2-ui/1-document/18-coordinates-document/1-get-document-scrolls/solution.md
2015-02-08 10:03:24 +03:00

747 B

  • `top` -- это `pageYOffset`.
  • `bottom` -- это `pageYOffset` плюс высота видимой части `documentElement.clientHeight`.
  • `height` -- полная высота документа, её вычисление давно в главе [](/metrics-window).

Итого:

function getDocumentScroll() {
  var scrollHeight = Math.max(
    document.body.scrollHeight, document.documentElement.scrollHeight,
    document.body.offsetHeight, document.documentElement.offsetHeight,
    document.body.clientHeight, document.documentElement.clientHeight
  );

  return {
    top: pageYOffset,
    bottom: pageYOffset + document.documentElement.clientHeight,
    height: scrollHeight
  };
}