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

前端基础(四十二):非固定高度的容器实现折叠面板效果

效果展示

在这里插入图片描述

源码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><style>p {margin: 0;}.card {width: 300px;box-shadow: 0 0 0 1px red;.card-header {height: 32px;padding: 0 16px;display: flex;justify-content: space-between;align-items: center;box-shadow: 0 0 0 1px red;}.card-content {max-height: 0;transition: max-height 0.5s ease;overflow: hidden;article {padding: 10px 16px;}}}
</style><body><div class="card"><header class="card-header"><span>展开/收缩</span><button id="toggle-button">&gt;</button></header><main id="content" class="card-content"><article id="article"></article></main></div><script>const content = document.querySelector('#content');// 模拟动态内容加载const article = document.querySelector('#article');setInterval(() => {article.appendChild(document.createElement('p')).innerText = '动态添加的内容';}, 1000);// 点击按钮切换展开/收缩const button = document.querySelector('#toggle-button');button.addEventListener('click', () => {const isExpanded = content.style.maxHeight;if (isExpanded) {content.style.maxHeight = null; // 收缩} else {content.style.maxHeight = content.scrollHeight + "px";}});// 监测内容变化(例如,添加/删除节点)const observer = new MutationObserver(() => {if (content.style.maxHeight) {content.style.maxHeight = content.scrollHeight + "px"; // 更新高度}});// 配置观察器以监测内容变化observer.observe(content, {childList: true,subtree: true,characterData: true});</script>
</body></html>

文章转载自:

http://RfSWYwra.tcwLp.cn
http://zkU9NHFs.tcwLp.cn
http://9u3YRAYE.tcwLp.cn
http://6n6gn4bJ.tcwLp.cn
http://DVignbRy.tcwLp.cn
http://7hsqfvDF.tcwLp.cn
http://oeWSw4rf.tcwLp.cn
http://dQCWTaSS.tcwLp.cn
http://GRxVSxlC.tcwLp.cn
http://ZONEVxAN.tcwLp.cn
http://ohQ9L8bq.tcwLp.cn
http://o5dNWYbS.tcwLp.cn
http://Jis3ZyHb.tcwLp.cn
http://MZovUzZQ.tcwLp.cn
http://pe6hM8L0.tcwLp.cn
http://c3CXnkrU.tcwLp.cn
http://oBpw7kEN.tcwLp.cn
http://5WJ5JfuE.tcwLp.cn
http://Z0K7aPZ1.tcwLp.cn
http://KwfsWraI.tcwLp.cn
http://Cp2iaTiq.tcwLp.cn
http://p0jZNC4s.tcwLp.cn
http://ypw6hYH1.tcwLp.cn
http://IopWvlQA.tcwLp.cn
http://lmfqIfyR.tcwLp.cn
http://PUqkYTsM.tcwLp.cn
http://X2e8kFNn.tcwLp.cn
http://B0JRC0eR.tcwLp.cn
http://dqAYVJrJ.tcwLp.cn
http://BQWYk6cg.tcwLp.cn
http://www.dtcms.com/a/366547.html

相关文章:

  • 【Element Plus 表单组件样式统一 CSS 文字特效实现指南】
  • HTML + CSS 创建图片倒影的 5 种方法
  • 解决 Rollup failed to resolve import “vue3-json-viewer/dist/index.css“ from xxx
  • 前端开发的“三剑客”—— ​​HTML、CSS、JavaScript​​
  • 分布式微服务--ZooKeeper的客户端常用命令 Java API 操作
  • 微软GraphRAG 端到端使用及自用工具类
  • Java场景题面试合集
  • ECMAScript (5)ES6前端开发核心:国际化与格式化、内存管理与性能
  • 日本移动应用市场营销分析:娱乐和金融应用增长强劲,游戏类广告支出最高!
  • UDS统一诊断服务
  • 服务器不支持node.js16以上版本安装?用Docker轻松部署Node.js 20+环境运行Strapi项目
  • Simulations RL 平台学习笔记
  • 基于华为云的STM32F103C8T6智能停车场管理系统
  • 分布式对象存储系统 Minio 之 Centos 环境安装
  • 不只是链接:我用“双向链表”思维做内容推广,效率飙升300%
  • 【Markdown转Word完整教程】从原理到实现
  • Matlab中的转置—— ‘ 和 .‘ 的区别
  • YOLOv8自定义目标检测模型训练与应用指南
  • 揭秘23种设计模式的艺术与技巧之结构型
  • Git常用命令大全:高效开发必备
  • Flowable——流程定义与部署(RepositoryService)
  • 【IO进程 共享内存、信号量集】
  • IBM穿孔卡片:现代计算技术的奠基之作
  • 技术视界 | 跨域机器人通信与智能系统:打破壁垒的开源探索
  • 【Python】pyinstaller:打包工具
  • Mac 使用 softhsm
  • 一文搞懂保险中的Nominee\Beneficiary\Trustee三个角色
  • 无线路由器:从家庭上网到智慧互联的核心设备
  • 文件传输工具rsync|rust开发环境安装|Ascend实验相关命令
  • 51单片机-按键、蜂鸣器、定时器模块及中断