getScroll.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.isWindow = isWindow;
  7. function isWindow(obj) {
  8. return obj !== null && obj !== undefined && obj === obj.window;
  9. }
  10. const getScroll = target => {
  11. var _a, _b;
  12. if (typeof window === 'undefined') {
  13. /* istanbul ignore next */
  14. return 0;
  15. }
  16. let result = 0;
  17. if (isWindow(target)) {
  18. result = target.pageYOffset;
  19. } else if (target instanceof Document) {
  20. result = target.documentElement.scrollTop;
  21. } else if (target instanceof HTMLElement) {
  22. result = target.scrollTop;
  23. } else if (target) {
  24. // According to the type inference, the `target` is `never` type.
  25. // Since we configured the loose mode type checking, and supports mocking the target with such shape below::
  26. // `{ documentElement: { scrollLeft: 200, scrollTop: 400 } }`,
  27. // the program may falls into this branch.
  28. // Check the corresponding tests for details. Don't sure what is the real scenario this happens.
  29. /* biome-ignore lint/complexity/useLiteralKeys: target is a never type */ /* eslint-disable-next-line dot-notation */
  30. result = target['scrollTop'];
  31. }
  32. if (target && !isWindow(target) && typeof result !== 'number') {
  33. result = (_b = ((_a = target.ownerDocument) !== null && _a !== void 0 ? _a : target).documentElement) === null || _b === void 0 ? void 0 : _b.scrollTop;
  34. }
  35. return result;
  36. };
  37. var _default = exports.default = getScroll;