getScroll.js 1.2 KB

1234567891011121314151617181920212223242526272829
  1. export function isWindow(obj) {
  2. return obj !== null && obj !== undefined && obj === obj.window;
  3. }
  4. export default function getScroll(target, top) {
  5. var _a, _b;
  6. if (typeof window === 'undefined') {
  7. return 0;
  8. }
  9. const method = top ? 'scrollTop' : 'scrollLeft';
  10. let result = 0;
  11. if (isWindow(target)) {
  12. result = target[top ? 'scrollY' : 'scrollX'];
  13. } else if (target instanceof Document) {
  14. result = target.documentElement[method];
  15. } else if (target instanceof HTMLElement) {
  16. result = target[method];
  17. } else if (target) {
  18. // According to the type inference, the `target` is `never` type.
  19. // Since we configured the loose mode type checking, and supports mocking the target with such shape below::
  20. // `{ documentElement: { scrollLeft: 200, scrollTop: 400 } }`,
  21. // the program may falls into this branch.
  22. // Check the corresponding tests for details. Don't sure what is the real scenario this happens.
  23. result = target[method];
  24. }
  25. if (target && !isWindow(target) && typeof result !== 'number') {
  26. result = (_b = ((_a = target.ownerDocument) !== null && _a !== void 0 ? _a : target).documentElement) === null || _b === void 0 ? void 0 : _b[method];
  27. }
  28. return result;
  29. }