Javascript可折叠面板默认情况下处于打开状态

时间:2018-09-05 10:52:19

标签: javascript html css

我正在遵循以下代码示例found here,以使用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/

在JSfiddle中,如果在“可折叠内容”和“默认显示的可折叠内容”之间交换div类,则会看到切换仅以一种方式起作用。

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

2 个答案:

答案 0 :(得分:2)

  

快速简便的解决方案:

使用下面说明的逻辑(答案的底部),我们可以通过简单地将最大高度的内联声明添加到“可折叠内容” 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,它将读取 computing 样式(包括内联和外部CSS)。由于我们无法直接更改计算样式,因此我们可以编辑内联css以覆盖最初声明的外部css(max-height: 100%)。当单击按钮时调用下一个“ if语句”时,计算出的样式将返回内联css。

答案 1 :(得分:1)

尝试一下

<script>

    function toggleCollapsibleSectionWithAnimation() {
     this.classList.toggle("collapsible-active");

        var buttonId = this.id;
        var sectionId = buttonId.replace("button","section");
        var content = document.getElementById(sectionId);

        var isDefaultMode = content.classList.contains('collapsible-content-shown-by-default');

    if (isDefaultMode) {
      content.classList.remove("collapsible-content-shown-by-default");
      content.style.maxHeight = 0;
    }

        if (content.style.maxHeight) {
          content.style.maxHeight = null;
        } else {
          content.style.maxHeight = content.scrollHeight + "px";
        }
    }</script>

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

<h1>
content
</h1>


</div>

我基本上将其设置为点击后模式。