useRect.ts 640 B

123456789101112131415161718192021222324252627282930313233
  1. import { Ref, unref } from 'vue';
  2. import { isWindow } from '/@/utils/is';
  3. export const useRect = (elementRef: (Element | Window) | Ref<Element | Window | undefined>) => {
  4. const element = unref(elementRef);
  5. if (isWindow(element)) {
  6. const width = element.innerWidth;
  7. const height = element.innerHeight;
  8. return {
  9. top: 0,
  10. left: 0,
  11. right: width,
  12. bottom: height,
  13. width,
  14. height,
  15. };
  16. }
  17. if (element && element.getBoundingClientRect) {
  18. return element.getBoundingClientRect();
  19. }
  20. return {
  21. top: 0,
  22. left: 0,
  23. right: 0,
  24. bottom: 0,
  25. width: 0,
  26. height: 0,
  27. };
  28. };