useDomWidth.ts 472 B

1234567891011121314151617181920212223
  1. import { ref, onMounted, onUnmounted } from 'vue';
  2. import { debounce } from 'lodash-es';
  3. /**
  4. * description: 获取页面宽度
  5. */
  6. export function useDomWidth() {
  7. const domWidth = ref(window.innerWidth);
  8. function resize() {
  9. domWidth.value = document.body.clientWidth;
  10. }
  11. onMounted(() => {
  12. window.addEventListener('resize', debounce(resize, 80));
  13. });
  14. onUnmounted(() => {
  15. window.removeEventListener('resize', resize);
  16. });
  17. return domWidth;
  18. }