4

Properties such as scrollTop or scrollLeft, as well as many other "old" (dhtml) properties now to-be-standardized in the CSSOM View module, still return integer rather than double (as specified in the draft) in high dpi devices.

For example on such devices if an HTML element is completely scrolled down, element.scrollTop + element.clientHeight is 1 less than element.scrollHeight even if visually it is completely scrolled. I suppose because the returned offset is rounded down.

Is there an alternative way to get scrollTop (not only on body but any scrollable element) complete of its fractional part?

Wes
  • 3,978
  • 4
  • 24
  • 45
  • Both `clientHeight` and `scrollHeight` should return an integer, and in fact the documentation states that *" The `scrollHeight` value is **equal to the minimum** `clientHeight` the element would require in order to fit all the content in the viewpoint without using a vertical scrollbar."*. If you want fractional values, you'd use `getBoundingClientRect()` instead. Not sure why there would be a difference of 1 pixel, other than maybe margin or something ? – adeneo Aug 10 '16 at 14:52
  • https://www.w3.org/TR/cssom-view-1/#extension-to-the-element-interface – Wes Aug 10 '16 at 14:54
  • and, getBoundingClientRect() doesn't return anything about scrolling offsets – Wes Aug 10 '16 at 14:55
  • What do you mean, the values will change as you scroll using `getBoundingClientRect`... to quote the [**docs**](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) once again *"The amount of scrolling that has been done of the viewport area (or any other scrollable element) is taken into account when computing the bounding rectangle. "* – adeneo Aug 10 '16 at 14:59
  • Also note that it's the recommended altenative on MDN for just [**scrollHeight**](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight) and [**clientHeight**](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight), which both returns integers, and should return the same values without differences, assuming you're checking the same thing. – adeneo Aug 10 '16 at 15:01
  • yeah. i didn't consider i could add a temporary element and check its position. thanks :P trying it... – Wes Aug 10 '16 at 15:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120615/discussion-between-wes-and-adeneo). – Wes Aug 10 '16 at 15:26

0 Answers0