我正在关注这个代码示例在这里找到 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(使用前将#替换为@)