news 2026/4/18 5:10:15

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/4/18 5:06:32

命令执行绕过

直接闹麻了 &#xff0c;命令执行绕不过空格的来了&#x1f923;&#xff0c;都能执行命令了&#xff0c;空格绕不过去直接全盘皆失赶紧补充一下自己的命令执行绕过知识&#x1f62d;空格绕过\t%09${IFS}$IFS$9$IFS%20{} 例如 &#xff1a;{cat,1.txt}<或是 << 例如 :…

作者头像 李华
网站建设 2026/4/18 6:28:21

大神优化 PDF工具箱神器,强烈推荐

PDF工具箱之前也给大家推荐过好&#xff0c;今天在给大家推荐一个非常好用功能有一些不一样的软件。 ABBYY FineReader PDF工具箱 这款PDF工具箱是俄罗斯大神优化出品的&#xff0c;功能强大&#xff0c;它集成了OCR 文字识别、文档处理、文件转换和索引、数据捕获、语言翻译等…

作者头像 李华
网站建设 2026/4/18 6:28:48

【光照】[PBR][法线分布]GGX实现方法对比

GGX的全称与基本概念‌GGX的全称‌&#xff1a;Ground Glass X (或 Generalized Trowbridge-Reitz Distribution)‌首次提出‌&#xff1a;Walter等人在2007年发表的论文《Microfacet Models for Refraction through Rough Surfaces》中提出GGX是一种‌法线分布函数‌(Normal D…

作者头像 李华
网站建设 2026/4/18 6:28:29

YC创业名录:精准对接高潜力初创公司

一、拆解需求 1. 产品核心定义&#xff1a;是什么、给谁用、做什么 产品本质&#xff1a;依托 Y Combinator&#xff08;YC&#xff09;投资生态的「高价值创业公司名录精准对接平台」&#xff0c;核心是整合 YC 自2005年以来投资的优质创业公司资源&#xff0c;提供多维度检索…

作者头像 李华
网站建设 2026/4/18 6:31:53

计算机毕设项目推荐—天气数据分析预测可视化分析预测系统

天气数据分析预测可视化分析预测系统 01开发环境 1.1 Python技术 1.2 MySQL数据库 1.3 B/S 结构 1.4 Vue.js 技术 02系统图片展示03代码展示 # 省份或者城市地图 # &#xff08;学习交流VX&#xff1a;S20231025S &#xff09; DEFAULT_HOST "https://assets.pyecharts…

作者头像 李华
网站建设 2026/3/27 23:21:03

基于Spring Boot的食品安全宣传网站(计算机毕设)

如需对应源码以及相应文档可私下方名片 ↓ 基于Spring Boot的食品安全宣传网站的设计与实现 摘 要 近几年频频发生的食品安全事故&#xff0c;主要是公众缺乏对食品安全的宣传教育。建立一套针对食品安全的宣传体系&#xff0c;既可以普及有关的政策&#xff0c;又能够提升大众…

作者头像 李华