getScroll.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getScroll;
  6. exports.isWindow = isWindow;
  7. function isWindow(obj) {
  8. return obj !== null && obj !== undefined && obj === obj.window;
  9. }
  10. function getScroll(target, top) {
  11. var _a, _b;
  12. if (typeof window === 'undefined') {
  13. return 0;
  14. }
  15. const method = top ? 'scrollTop' : 'scrollLeft';
  16. let result = 0;
  17. if (isWindow(target)) {
  18. result = target[top ? 'scrollY' : 'scrollX'];
  19. } else if (target instanceof Document) {
  20. result = target.documentElement[method];
  21. } else if (target instanceof HTMLElement) {
  22. result = target[method];
  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. result = target[method];
  30. }
  31. if (target && !isWindow(target) && typeof result !== 'number') {
  32. result = (_b = ((_a = target.ownerDocument) !== null && _a !== void 0 ? _a : target).documentElement) === null || _b === void 0 ? void 0 : _b[method];
  33. }
  34. return result;
  35. }