当前位置: 首页 > news >正文

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>
http://www.rkmt.cn/news/117683.html

相关文章:

  • 论文文献引用格式最新规范流出,毕业季限时必看!
  • SpringBoot使用设计模式一装饰器模式
  • 从零构建AI镜像,缓存命中率提升至95%的3个核心技巧
  • Java 大视界 -- Java 大数据在智能家居能源管理与节能优化中的深度应用
  • 基于java的SpringBoot/SSM+Vue+uniapp的仓储管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
  • 基于大数据的热点话题分析系统的设计与实开题报告 (1)
  • 如何解决 pip install 网络报错 ERROR: No matching distribution found for requests
  • FreeIPA能建立用户组,并将域组带入到加域的客户端
  • 基于大数据的热点话题分析系统的设计与实开题报告
  • 【值得收藏】RAG技术全解析:大模型检索增强生成的挑战、范式与优化策略
  • 超越规范:测试标准如何成为你的效率引擎,而非发展枷锁?
  • 你不知道的Q#调试黑科技:如何让Python端实时捕获量子状态异常
  • Windwos 内存被占用,但是任务管理器没有占用这么多,
  • 网络安全零基础入门怎么学?超详细学习指南在此,带你一站式从入门到精通!
  • Python蓝桥杯第三次学习+感悟
  • 量子开发环境依赖全解析,一文掌握VSCode高效配置秘诀
  • 内网渗透学习必备干货:12 个高价值靶场平台 + 实战避坑指南 + 操作技巧全收录!
  • 电信网络专线如何选择,才能满足企业需求?
  • 到底要不要 Vibe Coding ?
  • Paperzz AI:毕业论文写作的 “隐形助攻”,让学术输出告别 “抓瞎”
  • Windows找不到XAudio2_0.dll文件 如何下载修复?
  • 误删量子任务记录怎么办,3分钟极速恢复方案曝光
  • 一位网友逆向破解了 ChatGPT 记忆系统,给我干破防了
  • 【笔记】矩阵的谱半径
  • 当AI写作成为学术常态,我的论文却因“AIGC率”被拒?——一位留学生的“降重自救”实录与PaperXie的“人机协作”新解法
  • 不止于兼容:金仓数据库如何以部署、安全、性能三重革新重塑企业级数据库体验
  • 【Azure CLI量子作业日志分析秘籍】:5步精准定位量子计算异常根源
  • 酵母三杂交系统:解析多分子协同作用的分子互作研究利器
  • 多级菜单核心设计思路 + 逐步扩充示例【20251217】001篇
  • THUPC2026 邮寄