getUnboundedScrollPosition.js.flow 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule getUnboundedScrollPosition
  8. * @typechecks
  9. */
  10. 'use strict';
  11. /**
  12. * Gets the scroll position of the supplied element or window.
  13. *
  14. * The return values are unbounded, unlike `getScrollPosition`. This means they
  15. * may be negative or exceed the element boundaries (which is possible using
  16. * inertial scrolling).
  17. *
  18. * @param {DOMWindow|DOMElement} scrollable
  19. * @return {object} Map with `x` and `y` keys.
  20. */
  21. function getUnboundedScrollPosition(scrollable) {
  22. if (scrollable.Window && scrollable instanceof scrollable.Window) {
  23. return {
  24. x: scrollable.pageXOffset || scrollable.document.documentElement.scrollLeft,
  25. y: scrollable.pageYOffset || scrollable.document.documentElement.scrollTop
  26. };
  27. }
  28. return {
  29. x: scrollable.scrollLeft,
  30. y: scrollable.scrollTop
  31. };
  32. }
  33. module.exports = getUnboundedScrollPosition;