VueUse useResizeObserver 监控元素大小的变化
<template>
<textarea ref="el" rows="5" cols="30">
缩放这个输入框试试看
{{text}}</textarea>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useResizeObserver } from '@vueuse/core'
//元素对象
const el = ref(null);
//元素响应式对象
const text = ref('')
useResizeObserver(el, (entries) => {
const entry = entries[0]
const { width, height } = entry.contentRect
text.value = `width: ${width}, height: ${height}`
})
</script>
例子