news 2026/8/7 0:19:57

Vue3利用ResizeObserver监听Textarea的尺寸动态调整表格tbody的maxHeight

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Vue3利用ResizeObserver监听Textarea的尺寸动态调整表格tbody的maxHeight

调整表格tbody的maxHeight推荐方式是直接修改css,本文主要描述的是不推荐但使用ResizeObserver再进一步修改dom的maxHeight(之所以选择ResizeObserver这个API是因为Textarea默认没有resize事件),从而达到不溢出可视窗口,表格内容区域自适应并纵向滚动

一、直接修改行内样式

<template> <div ref="contentRef"> <my-content class="content"> <top-info class="top-info" v-model:loading="topLoading" v-model:isEdit="isEdit" /> <my-list /> </my-content> </div> </template> <script lang="ts" setup> import { CSSProperties } from 'vue'; import { nextTick, watch, onUnmounted } from 'vue'; import { debounce } from 'lodash-es'; import MyList from './MyList.vue'; import TopInfo from './TopInfo.vue'; const contentRef = ref(); const topLoading = ref(false); const isEdit = ref(false); const getContentDom = () => contentRef.value.querySelector('.content'); const getTbodyDom = () => getContentDom().querySelector('.my-table-body'); const getTopDom = () => getContentDom().querySelector('.top-info'); const getTopTextarea = () => getTopDom().querySelector('textarea'); const handleTableMaxHeight = debounce(() => { const contentDom = getContentDom(); const tbodyDom = getTbodyDom(); if (tbodyDom) { const topDom = getTopDom(); const tableMaxHeight1 = contentDom.clientHeight - topDom.scrollHeight - 180; // 为了展示逻辑下方直接修改样式,可以传值,有的table插件有maxHeight参数有此类效果,如果maxHeight参数没有响应式效果再考虑直接操作dom样式 tbodyDom.style.maxHeight = tableMaxHeight1 + 'px'; tbodyDom.style.overflowY = 'auto'; } }, 200); const watchTextarea = debounce(() => { const textarea = getTopTextarea(); const resizeObserver = new ResizeObserver((entries) => { for (let entry of entries) { const { width, height } = entry.contentRect; console.log('New dimensions:', width, height); // 处理尺寸变化,例如更新数据或执行其他操作 } nextTick(() => { handleTableMaxHeight(); }); }); resizeObserver.observe(textarea); textarea.__resizeObserver__ = resizeObserver; // 保存引用以便之后可以访问或清理(可选) }, 200); onUnmounted(() => { const textarea = getTopTextarea(); if (textarea.__resizeObserver__) { textarea.__resizeObserver__.disconnect(); } }); watch( () => [isEdit.value, topLoading.value], () => { nextTick(() => { handleTableMaxHeight(); const textarea = getTopTextarea(); if (textarea) { watchTextarea(); } }); }, { deep: true, immediate: true } ); </script>

二、利用组件style对象传参修改css的important样式

比style行内样式优先级高的是css的important样式,使用scss或者less的var函数,可以传递获取tbody的maxHeight

<template> <div ref="contentRef"> <my-content class="content"> <top-info class="top-info" v-model:loading="topLoading" v-model:isEdit="isEdit" /> <my-list :style="tbodyStyle"/> </my-content> </div> </template> <script lang="ts" setup> import { CSSProperties } from 'vue'; import { nextTick, watch, onUnmounted } from 'vue'; import { debounce } from 'lodash-es'; import MyList from './MyList.vue'; import TopInfo from './TopInfo.vue'; const contentRef = ref(); const topLoading = ref(false); const isEdit = ref(false); const tbodyStyle = ref<any>({ padding: 0 }); const getContentDom = () => contentRef.value.querySelector('.content'); const getTbodyDom = () => contentRef.value.querySelector('.my-table-body'); const getTopTextarea = () => getTopDom().querySelector('textarea'); const handleTableMaxHeight = debounce(() => { const contentDom = getContentDom(); const topDom = getTopDom(); const tableMaxHeight1 = contentDom.clientHeight - topDom.scrollHeight - 180; tbodyStyle.value['--tbody-max-height'] = tableMaxHeight1 + 'px'; }, 200); const watchTextarea = debounce(() => { const textarea = getTopTextarea(); const resizeObserver = new ResizeObserver((entries) => { for (let entry of entries) { const { width, height } = entry.contentRect; console.log('New dimensions:', width, height); // 处理尺寸变化,例如更新数据或执行其他操作 } nextTick(() => { handleTableMaxHeight(); }); }); resizeObserver.observe(textarea); textarea.__resizeObserver__ = resizeObserver; // 保存引用以便之后可以访问或清理(可选) }, 200); onUnmounted(() => { const textarea = getTopTextarea(); if (textarea.__resizeObserver__) { textarea.__resizeObserver__.disconnect(); } }); watch( () => [isEdit.value, topLoading.value], () => { nextTick(() => { handleTableMaxHeight(); const textarea = getTopTextarea(); if (textarea) { watchTextarea(); } }); }, { deep: true, immediate: true } ); </script> <style lang="less" scoped> :deep(.my-table-body) { max-height: var(--tbody-max-height) !important; overflowY: auto; } </style>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/7 1:44:41

桥梁与隧道安全守护者 抗冰冻型风速监测方案

强风是威胁大型桥梁、高山隧道口安全运营的重要自然因素。实时、准确的风速风向监测是发布预警、采取限行措施的科学依据。FST200-207抗冰冻型超声波风速风向传感器专为此类基础设施的安全监测而设计。 桥梁&#xff0c;特别是悬索桥和斜拉桥&#xff0c;对风荷载非常敏感。在…

作者头像 李华
网站建设 2026/8/5 8:14:37

05-FreeRTOS的内存管理

概述在 FreeRTOS 中&#xff0c;内存管理是连接内核功能与硬件资源的核心环节&#xff0c;直接影响系统的实时性、稳定性和资源利用率。对于基于 STM32 的开发&#xff0c;理解 FreeRTOS 的 内存管理方案是实现可靠嵌入式系统的基础。一、为什么要学习 FreeRTOS 内存管理&#…

作者头像 李华
网站建设 2026/8/7 11:08:46

基于大数据的人脸识别系统设计与实现开题报告

选题意义随着科技的发展&#xff0c;人脸识别技术被广泛应用在门禁、支付、身份验证等领域&#xff0c;极大地提高了效率并增强了安全性。它能减少传统密码式的繁琐&#xff0c;让人们的生活更便捷。这是数字化转型的一部分&#xff0c;有助于构建智慧城市和物联网环境下的智能…

作者头像 李华
网站建设 2026/8/7 23:00:09

车载 Android 系统稳定性问题全解析:从性能到黑屏的排查指南

引言 在车载 Android 系统开发中,稳定性问题是最让人头疼的挑战之一。与手机不同,车载系统对稳定性的要求近乎苛刻——想象一下,用户正在高速公路上行驶,导航突然黑屏,或者中控卡死无响应,这不仅仅是用户体验问题,更关乎行车安全。 经过多年的车载系统开发实践,我们将…

作者头像 李华
网站建设 2026/8/7 23:49:14

气象在线监测系统助力智慧环境管理,金叶仪器专业气象监测解决方案

在当今社会&#xff0c;气象环境数据对于生产生活、科学研究与可持续发展具有日益重要的意义。准确、连续的气象监测不仅能够帮助人们更好地理解自然环境变化&#xff0c;也为农业、交通、能源、环保等多个领域提供了关键的数据支撑。随着物联网、云计算等技术的成熟&#xff0…

作者头像 李华