Javascript 可折叠面板默认打开

2024-05-11

我正在关注这个代码示例在这里找到 https://www.w3schools.com/howto/howto_js_collapsible.asp使用 css/html/javascript 创建可折叠面板:

function toggleCollapsibleSectionWithAnimation() {
  this.classList.toggle("collapsible-active");
  var buttonId = this.id;
  var sectionId = buttonId.replace("button","section");
  var content = document.getElementById(sectionId);
  if (content.style.maxHeight) {
    content.style.maxHeight = null;
  } else {
    content.style.maxHeight = content.scrollHeight + "px";
  }
}
/* Style the button that is used to open and close the collapsible content */
.collapsible {
  background-color: transparent;
  color: #444;
  cursor: pointer;
  width: auto;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.collapsible-active, .collapsible:hover {
  text-decoration: underline;
}

/* Style the collapsible content. Note: hidden by default */
.collapsible-content {
  max-height: 0;
  padding: 10px;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}

/* Style the collapsible content. Note: shown by default */
.collapsible-content-shown-by-default {
  max-height: 100%;
  padding: 10px;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}
<button class="collapsible" id="collapsible-button-0" onclick="toggleCollapsibleSectionWithAnimation.call(this)"><b>Model</b> (show/hide)</button>
<div class="collapsible-content" id="collapsible-section-0">
  <h1>
  content
  </h1>
</div>

此代码有效,默认情况下可折叠部分将被隐藏。

我想扭转这一点,以便默认显示我的面板,但它的行为方式应该完全相同,单击可折叠按钮将打开或关闭下面的部分。

不确定如何反转起始变量。当它开始隐藏时,我们需要在内容面板上从 max-height 0 开始。在 javascript 中,此值更改为 content.scrollHeight 以打开面板。

这是一个 JSfiddle 示例:https://jsfiddle.net/trbk5vwg/9/ https://jsfiddle.net/trbk5vwg/9/

在 JSfiddle 中,如果您在“collapsible-content”和“collapsible-content-shown-by-default”之间交换 div 类,您将看到切换仅以一种方式工作。

我不确定如何在CSS中获取scrollHeight,因此不确定如何初始化默认显示的面板的maxHeight(100%不起作用)。


快速简单的解决方案:

使用下面解释的逻辑(答案底部),我们可以通过简单地将 max-height 的内联声明添加到“collapsible-content”div 来解决问题:

<div class="collapsible-content" id="collapsible-section-0" style="max-height: 100%">

JavaScript 解决方案:

工作代码是:

/* Style the button that is used to open and close the collapsible content */
.collapsible {
  background-color: transparent;
  color: #444;
  cursor: pointer;
  width: auto;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.collapsible-active, .collapsible:hover {
  text-decoration: underline;
}

/* Style the collapsible content. Note: hidden by default */
.collapsible-content {
  max-height: 100%;
  padding: 10px;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}

/* Style the collapsible content. Note: shown by default */
.collapsible-content-shown-by-default {
  max-height: 100%;
  padding: 10px;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}
<script>
	function toggleCollapsibleSectionWithAnimation() {
		this.classList.toggle("collapsible-active");
		var buttonId = this.id;
		var sectionId = buttonId.replace("button","section");
		var content = document.getElementById(sectionId);
    var mHeight = window.getComputedStyle(content).maxHeight;
		if (mHeight !== "0px"){
		  content.style.maxHeight = "0px";
		} else {
		  content.style.maxHeight = "100%";
		}
	}</script>

<button class="collapsible" id="collapsible-button-0" onclick="toggleCollapsibleSectionWithAnimation.call(this)"><b>Model</b> (show/hide)</button>
<div class="collapsible-content" id="collapsible-section-0">

<h1>
content
</h1>


</div>

为什么这些有效:

您不能在 css 文件中将 max-height 更改为 100% 的原因:在原始 JSFiddle 中,单击按钮时的第一个“if 语句”正在读取content.style.maxHeight. .style实际上读取内联 css,在原始情况下,这意味着它将 maxHeight 读取为 null。当将外部 css(非内联)更改为 100% 时,.style.maxHeight函数仍将 maxHeight 读取为 null。

要在 html/css 中修复此问题,我们只需将初始内联 css 设置为“max-height: 100%”,因此content.style.maxHeight函数将返回正确的值。

为了在 JS 中解决这个问题,我们可以使用window.getComputedStyle(content).maxHeight,这将读取computed样式(包括内联和外部 CSS)。因为我们无法直接更改计算样式,所以我们可以编辑内联 css 来覆盖最初声明的外部 css (max-height: 100%)。当单击按钮时调用下一个“if 语句”时,计算的样式将返回内联 css。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript 可折叠面板默认打开 的相关文章

  • 如何获取node.js中调用函数的文件路径?

    以下是来自三个文件的一些示例代码 foo js var myFunc require myFunc function foo myFunc message bar js var myFunc require myFunc function
  • 在纯 css3 循环中按顺序对元素进行动画处理

    我正在尝试在完整的 css3 动画中按顺序对元素进行动画处理 似乎非常直接的答案是使用动画延迟 但是我想要这个循环 有什么想法如何使动画无限循环吗 我找到了这个fiddle http jsfiddle net cherryflavourpe
  • Vue.js:折叠/展开父级中的所有元素

    我需要为我的 Vue 组件 一些可折叠面板 添加 展开 折叠全部 功能 如果用户单击折叠按钮 然后单击某个面板并将其展开 然后单击折叠按钮不会做任何事因为观看的参数不会改变 那么如何正确实现此功能 按钮必须始终折叠和展开组件 我准备了简单的
  • selenium webdriver 管理器更新 - npm

    我尝试使用 webdriver manager 更新 selenium webdriver 但出现错误 Error Got error Error read ECONNRESET from https selenium release st
  • JS - 文件读取器 API 获取图像文件大小和尺寸

    您好 我正在使用以下代码来使用文件读取器 API 获取上传图像
  • 这种日期时间格式有简单的转换吗?

    我正在使用 jQuery 从 JSON feed 中检索数据 并且作为 feed 的一部分 我获得了 datetime 属性 例如 2009 07 01 07 30 09 我想将此信息放入 javascript Date 对象中以方便使用
  • JavaScript 检查 null 与 undefined 以及 == 和 === 之间的区别

    如何检查变量是否null or undefined和有什么区别null and undefined 有什么区别 and 很难在 Google 上搜索 如何检查变量是否null or undefined 是变量null if a null o
  • 通过 Scriptaculous 拖放防止 JavaScript 点击事件

    我的页面上有一些可拖动的元素 这些相同的元素有一个导航到另一个页面的点击事件 我试图确定在用户拖动时防止触发单击事件的最佳方法 但如果不拖动则仍然允许单击事件 有人对实现这一目标的最佳方法有任何想法吗 我通过使用类似以下内容解决了这个问题
  • 客户端与服务器端图像压缩[关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在研究用户可以上传图片的东西 图像大小不受限制 现在我有两个选择使用PHP 服务器端 压缩图像或使用
  • 在 contenteditable div 中的插入符处插入 html

    我有一个带有 contenteditable 设置的 div 并且我正在使用 jquery 捕获按键 以在按下 Enter 键时调用 PreventDefault 如同这个问题 https stackoverflow com questio
  • 包含菜单按钮的 Ag-grid 单元格

    我在我的项目中使用社区版本的 ag grid 我正在尝试在每一行的一个单元格中添加菜单按钮 单击菜单按钮时 应该会弹出菜单 其中包含编辑 删除 重命名选项 并且当单击菜单上的任何项目时 我需要触发具有行值的事件 我正在尝试创建一个将显示按钮
  • 三行无表 CSS 布局,中间行填充剩余空间

    我需要的是一个包含 3 行的基于像素高度的 div 最上面一行的高度根据内容而变化 底行具有固定高度 中间行填充任何剩余空间 一切都是宽度 100 我一直在努力构建一个 div 和基于 CSS 的布局几个小时 这让我从字面上看seconds
  • 在 IOS 设备上制作动画时,2 个相互堆叠的动画元素会发生变化(z 索引位置)吗?

    JSFIDDLE http fiddle jshell net 6gdrQ 18 我有 2 个动画元素 一种是简单的旋转脚本 它像硬币一样旋转徽标的中间部分 另一个动画是中间部分翻转时您看到的徽标后面的粒子画布烟雾动画 我遇到的问题是画布烟
  • 为什么我不能分配 const 但我可以控制台记录它?

    我做了一些java脚本练习 让几个链接按字母顺序排列 这是 HTML a href a is good a a href c is good a a href b is good a JavaScript const allhref doc
  • jQuery find() 只返回第一个匹配的结果?

    我在 jQuery 中使用 find 方法 但无法获得与选择器条件匹配的所有结果 这是我的 HTML div class something div
  • 在 Bootstrap 按钮下拉列表标题/占位符文本中显示所选项目

    这个问题已经在 Stackoverflow 上被问过几次了 但是我仍然无法弄清楚它的真相 而且我的查询正在抛出更多的下拉菜单 所以我有两个下拉菜单和一个搜索 我想从下拉列表和 选定 中进行选择以替换下拉占位符文本 但我还需要记住 点击搜索后
  • AngularJS:如何通过 websocket 发送文件?

    我是 websocket 的新手 我被分配了一个现有的工作聊天模块 目前该模块仅向其他用户发送消息 我被要求集成用户可以互相发送 附件 的功能 供参考 我发现了这个链接 https stackoverflow com questions 1
  • 如何在二维数组中找到字符串?

    我有一个看起来像这样的数组 var array a b c d e f 我希望能够在数组中搜索字符串 d 并返回对应的值 c try function find str array for var i in array if array i
  • HTML5 中填充笔划的透明度

    我正在 HTML5 中开发一个涂鸦应用程序 我想做一种桶功能 这个想法是绘制一条路径 它将被关闭并用选定的颜色 描边的颜色 填充 它对于纯色效果很好 但如果我想要透明的描边和填充 我会遇到这个问题 所发生的情况是填充完成到笔划的中间 路径的
  • 父元素的 mousedown 事件中的 offsetX 和 offsetY 错误

    我在 mousedown 上获取 offsetX 时遇到问题 下面是我的代码 div Click the text The mouseDown function is triggered when the mouse button is p

随机推荐