getScroll.js 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. export function isWindow(obj) {
  2. return obj !== null && obj !== undefined && obj === obj.window;
  3. }
  4. const getScroll = target => {
  5. var _a, _b;
  6. if (typeof window === 'undefined') {
  7. /* istanbul ignore next */
  8. return 0;
  9. }
  10. let result = 0;
  11. if (isWindow(target)) {
  12. result = target.pageYOffset;
  13. } else if (target instanceof Document) {
  14. result = target.documentElement.scrollTop;
  15. } else if (target instanceof HTMLElement) {
  16. result = target.scrollTop;
  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. /* biome-ignore lint/complexity/useLiteralKeys: target is a never type */ /* eslint-disable-next-line dot-notation */
  24. result = target['scrollTop'];
  25. }
  26. if (target && !isWindow(target) && typeof result !== 'number') {
  27. result = (_b = ((_a = target.ownerDocument) !== null && _a !== void 0 ? _a : target).documentElement) === null || _b === void 0 ? void 0 : _b.scrollTop;
  28. }
  29. return result;
  30. };
  31. export default getScroll;