【Vue3】实现一个超过高度后可控制显示隐藏的组件

ops/2025/3/6 9:42:57/

组件效果图

  • 未达到最大高度

在这里插入图片描述

  • 达到设置的最大高度
    在这里插入图片描述
  • 进行展开
    在这里插入图片描述

实现代码

  • 组件代码

备注:通过tailwindcss设置的样式,通过@element-plus/icons-vue设置的图标,可根据情况进行替换

<template><!-- 限制高度组件 --><div ref="restrictionBox" class="relative overflow-hidden bg-blue-300" :class="control.isUnfold ? '' : max"><div ref="restrictionChil"><slot></slot></div><!-- 渐变 --><div v-if="control.isExceed && !control.isUnfold" class="absolute inset-x-0 bottom-0 h-12 bg-gradient-to-t from-white"></div></div><!-- 控制按钮 --><div v-if="control.isExceed"><div class="flex h-12 cursor-pointer select-none items-center justify-center space-x-1 pb-2 text-blue-500 active:text-blue-400" @click="changeUnfold"><span class="text-sm">{{ control.isUnfold ? '收起' : '展开' }}</span><el-icon><ArrowUp v-if="control.isUnfold" /><ArrowDown v-else /></el-icon></div></div>
</template><script setup>javascript">
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { ArrowUp, ArrowDown } from '@element-plus/icons-vue'// 接收参数
const props = defineProps({// 最大高度 - 收起前max: {type: String,default: 'max-h-48'}
})// 组件控制参数
const control = reactive({boxHeight: 0,chilHeight: 0,isExceed: false, // 是否超出高度isUnfold: false // 是否展开
})// 改变展开方式
const changeUnfold = () => (control.isUnfold = !control.isUnfold)// 获取元素
const restrictionBox = ref(null)
const restrictionChil = ref(null)// 创建高度监听 及监听销毁
let observerBox = null
let observerChil = null// 销毁监听
const destroyedObserver = () => {if (observerBox) {observerBox.disconnect()observerBox = null}if (observerChil) {observerChil.disconnect()observerChil = null}
}// 比较高度的函数
const compareHeights = () => {if (control.boxHeight > 0 && control.chilHeight > 0) {// 高度超出,出现下拉if (control.chilHeight > control.boxHeight) {destroyedObserver()console.log('超出高度')control.isExceed = true}}
}// 页面加载完成
onMounted(() => {// 父级监听observerBox = new ResizeObserver(entries => {entries.forEach(entry => {control.boxHeight = entry.contentRect.heightcompareHeights()})})// 内容监听observerChil = new ResizeObserver(entries => {entries.forEach(entry => {control.chilHeight = entry.contentRect.heightcompareHeights()})})// 开始监听两个元素observerBox.observe(restrictionBox.value)observerChil.observe(restrictionChil.value)
})
onUnmounted(() => {destroyedObserver()
})
</script>

外层引用

<script setup>javascript">
import HeightRestriction from '../../components/HeightRestriction/HeightRestriction.vue'const ttt1 = ref(0)
const tttt = () => {ttt1.value++
}
</script><template><HeightRestriction><button @click="tttt">测试</button><div v-for="i in ttt1" :key="i">{{ 'ttt1ttt1ttt1' }}</div></HeightRestriction>
</template>

http://www.ppmy.cn/ops/163565.html

相关文章

smart代理原生住宅IP是如何避免跨境电商店铺被DDoS攻击的?

随着跨境电商的迅猛发展&#xff0c;越来越多的商家开始把业务拓展到国际市场&#xff0c;然而&#xff0c;随之而来的是网络安全问题的威胁&#xff0c;其中最常见的是DDoS攻击。 这种攻击方式会让商家的网站或应用程序停止运作&#xff0c;从而影响销售和声誉。 幸运的是&…

CES Asia 2025增设未来办公教育板块,科技变革再掀高潮

作为亚洲消费电子领域一年一度的行业盛会&#xff0c;CES Asia 2025&#xff08;第七届亚洲消费电子技术贸易展&#xff09;即将盛大启幕。今年展会规模再度升级&#xff0c;预计将吸引超过500家全球展商参展&#xff0c;专业观众人数有望突破10万。除了聚焦人工智能、物联网、…

【智能体架构:Agent】LangChain智能体类型ReAct、Self-ASK的区别

1. 什么是智能体 将大语言模型作为一个推理引擎。给定一个任务&#xff0c; 智能体自动生成完成任务所需步骤&#xff0c; 执行相应动作&#xff08;例如选择并调用工具&#xff09;&#xff0c; 直到任务完成。 2. 先定义工具&#xff1a;Tools 可以是一个函数或三方 API也…

【AI深度学习基础】Pandas完全指南进阶篇:解锁高效数据处理高阶技能 (含完整代码)

&#x1f4da; Pandas 系列文章导航 入门篇 &#x1f331;进阶篇 &#x1f680;终极篇 &#x1f30c; &#x1f31f; 前言 在入门篇中&#xff0c;我们掌握了 Pandas 的核心数据结构和基础操作。本篇将深入探索 高效数据处理、复杂数据建模、性能调优 等进阶技能&#xff0c;…

Android14 OTA差分包升级报kPayloadTimestampError (51)

由于VF 架构&#xff0c; 所以镜像的打包时间可能存在偏差&#xff0c; 如 boot.img 和 客制化的一些镜像打包 可能会在 vendor 侧进行打包。 而 与system 侧进行merge 时&#xff0c;时间戳比较乱&#xff0c;为了解决这个问题&#xff0c;让时间戳进行统一。 使用adb方式验证…

火语言RPA--删除PDF页

【组件功能】&#xff1a;删除PDF指定页文档 配置预览 配置说明 文件路径 支持T或# 默认FLOW输入项 待删除PDF页的PDF文件完整路径。 删除页码 支持T或# 要删除的页面&#xff0c;多个用逗号分隔&#xff0c;连续删除多页用“-”连接起始结尾&#xff0c;例如&#xff1a;…

Python数据可视化——Matplotlib的基本概念和使用

Matplotlib是Python中最常用的绘图和数据可视化库之一&#xff0c;能够帮助我们将数据以图表的形式展示出来&#xff0c;使数据的规律和趋势更加直观。无论是在科学计算、工程分析&#xff0c;还是商业报告和机器学习领域&#xff0c;Matplotlib都能发挥重要作用。 Matplotlib…

服务器内存

1、cpu对内存的支持 有两个内存控制器&#xff08;IMC&#xff0c;Integrate Memory Controller&#xff09;。 每个内存控制器上都有一个 DDR PHY。DDR PHY 是连接 DDR 内存条和内存控制器的桥梁。它负在内存控制器和内存条之间做协议信号的转换。 每个 DDR PHY 有 3 个 chann…