getUnboundedScrollPosition.js 996 B

123456789101112131415161718192021222324252627282930313233343536
  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. * @typechecks
  8. */
  9. 'use strict';
  10. /**
  11. * Gets the scroll position of the supplied element or window.
  12. *
  13. * The return values are unbounded, unlike `getScrollPosition`. This means they
  14. * may be negative or exceed the element boundaries (which is possible using
  15. * inertial scrolling).
  16. *
  17. * @param {DOMWindow|DOMElement} scrollable
  18. * @return {object} Map with `x` and `y` keys.
  19. */
  20. function getUnboundedScrollPosition(scrollable) {
  21. if (scrollable.Window && scrollable instanceof scrollable.Window) {
  22. return {
  23. x: scrollable.pageXOffset || scrollable.document.documentElement.scrollLeft,
  24. y: scrollable.pageYOffset || scrollable.document.documentElement.scrollTop
  25. };
  26. }
  27. return {
  28. x: scrollable.scrollLeft,
  29. y: scrollable.scrollTop
  30. };
  31. }
  32. module.exports = getUnboundedScrollPosition;