useAutoAdapt.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { ref } from 'vue';
  2. import { ScreenSizeEnum } from '/@/enums/sizeEnum';
  3. import { useWindowSizeFn } from '/@/hooks/event/useWindowSizeFn';
  4. // 定义 useAdapt 方法参数
  5. interface AdaptOptions {
  6. // xl>1200
  7. xl?: string | number;
  8. // xl>992
  9. lg?: string | number;
  10. // xl>768
  11. md?: string | number;
  12. // xl>576
  13. sm?: string | number;
  14. // xl>480
  15. xs?: string | number;
  16. //xl<480默认值
  17. mindef?: string | number;
  18. //默认值
  19. def?: string | number;
  20. }
  21. export function useAdapt(props?: AdaptOptions) {
  22. //默认宽度
  23. const width = ref<string | number>(props?.def || '600px');
  24. //获取宽度
  25. useWindowSizeFn(calcWidth, 100, { immediate: true });
  26. //计算宽度
  27. function calcWidth() {
  28. let windowWidth = document.documentElement.clientWidth;
  29. switch (true) {
  30. case windowWidth > ScreenSizeEnum.XL:
  31. width.value = props?.xl || '600px';
  32. break;
  33. case windowWidth > ScreenSizeEnum.LG:
  34. width.value = props?.lg || '600px';
  35. break;
  36. case windowWidth > ScreenSizeEnum.MD:
  37. width.value = props?.md || '600px';
  38. break;
  39. case windowWidth > ScreenSizeEnum.SM:
  40. width.value = props?.sm || '500px';
  41. break;
  42. case windowWidth > ScreenSizeEnum.XS:
  43. width.value = props?.xs || '400px';
  44. break;
  45. default:
  46. width.value = props?.mindef || '300px';
  47. break;
  48. }
  49. }
  50. return { width, calcWidth };
  51. }