嵌套的Accordions在Javascript中重叠

时间:2018-06-18 16:14:56

标签: javascript html css

我正在尝试使用CSS,html和Javascript创建一些嵌套的手风琴。一切都按预期工作,直到我试图添加滑动动画,它们确实滑动,但现在没有调整maxHeight以考虑其内部元素的高度,因此元素重叠并且无法正常看到。

我可以仅在按钮处于活动状态时更新每个元素的maxHeight以获得所需的结果,如果是,那么最好的方法是什么?

var buttonListLB = document.getElementsByClassName("button-lightblue");
var buttonListB = document.getElementsByClassName("button-blue");
var buttonListDB = document.getElementsByClassName("button-darkblue");
var i;
var j;
var k;

function openDropbox() {
  this.classList.toggle("active");
  var panel = this.nextElementSibling;

  if (panel.style.display === "block") { //The parent's status should be considered here
    panel.style.display = "none";
    panel.style.maxHeight = null;
  } else {
    panel.style.display = "block";
    panel.style.maxHeight = (panel.scrollHeight) + "px";
  }
}

for (i = 0; i < buttonListLB.length; i++) {
  for (j = 0; j < buttonListB.length; j++) {
    for (k = 0; k < buttonListDB.length; k++) {
      buttonListDB[k].addEventListener("click", openDropbox);
    }
    buttonListB[j].addEventListener("click", openDropbox);
  }
  buttonListLB[i].addEventListener("click", openDropbox);
}
.column {
  float: left;
  width: 48%;
  padding: 2px;
  height: auto;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

.button-lightblue {
  background-color: #0287c5;
  color: white;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: 1px solid white;
  border-radius: 6px;
  text-align: left;
  outline: none;
  font-size: 15px;
  font-family: Arial;
  font-weight: bold;
}

.button-lightblue:after {
  content: '⏵';
  font-size: 12px;
  font-family: Arial;
  font-weight: normal;
  color: white;
  float: right;
  margin-left: 2px;
}

.active,
.button-lightblue:hover {
  background-color: #0085d8;
}

.active:after {
  content: "⏷";
}

.button-blue {
  background-color: #005aab;
  color: white;
  cursor: pointer;
  padding: 16px;
  width: 100%;
  border: 2px solid white;
  border-radius: 6px;
  text-align: left;
  outline: none;
  font-size: 15px;
  font-family: Arial;
  font-weight: bold;
}

.button-blue:after {
  content: '⏵';
  font-size: 12px;
  font-family: Arial;
  font-weight: normal;
  color: white;
  float: right;
  margin-left: 2px;
}

.active,
.button-blue:hover {
  background-color: #0085d8;
}

.active:after {
  content: "⏷";
}

.button-darkblue {
  background-color: #003c71;
  color: white;
  cursor: pointer;
  padding: 14px;
  width: 100%;
  border: 2px solid white;
  border-radius: 6px;
  text-align: left;
  outline: none;
  font-size: 15px;
  font-family: Arial;
  font-weight: bold;
}

.button-darkblue:after {
  content: '⏵';
  font-size: 12px;
  font-family: Arial;
  font-weight: normal;
  color: white;
  float: right;
  margin-left: 2px;
}

.active,
.button-darkblue:hover {
  background-color: #0085d8;
}

.active:after {
  content: "⏷";
}

.button-link {
  padding: 14px;
  background-color: #f1f1f1;
  height: 0;
  color: black;
  cursor: pointer;
  text-align: left;
  font-size: 12px;
  font-family: Arial;
}

.accordion-content {
  padding: 0 2px;
  background-color: white;
  max-height: 0;
  font-family: Arial;
  display: none;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}

.dropbox-content {
  position: fixed;
  padding: 0 2px;
  background-color: #f1f1f1;
  max-height: 0;
  display: none;
  width: 35%;
  z-index: 1;
  transition: max-height 0.2s ease-out;
}

.dropbox-content a {
  color: #0085d8;
  font-family: Arial;
  transition: max-height 0.4s ease-out;
}

.dropbox-content font {
  color: #0085d8;
  font-family: Arial;
  transition: max-height 0.4s ease-out;
}

.additional-content {
  padding: 5px 5px;
  display: block;
  background-color: #003c71;
  font-family: Arial;
  color: white;
  overflow: visible;
}
<div class="row">
  <div class="column">
    <button class="button-lightblue"> Top Button 1</button>
    <div class="accordion-content">
      <button class="button-blue"> Middle Button 1</button>
      <div class="accordion-content">
        <button class="button-darkblue"> Bottom Button 1</button>
        <div class="accordion-content">
          <font face="Arial">
            <a href="" class="button-link" target="_blank">Dead Link</a>
          </font>
        </div>
        <button class="button-darkblue"> Bottom Button 2</button>
        <div class="accordion-content">
          <font face="Arial">
            <a href="" class="button-link" target="_blank">Dead Link</a>
          </font>
        </div>
      </div>
      <button class="button-blue"> Middle Button 2</button>
      <div class="accordion-content">
        <button class="button-darkblue"> Bottom Button 3</button>
        <div class="accordion-content">
          <font face="Arial">
            <a href="" class="button-link" target="_blank">Dead Link</a>
          </font>
        </div>
        <button class="button-darkblue"> Bottom Button 4</button>
        <div class="accordion-content">
          <font face="Arial">
            <a href="" class="button-link" target="_blank">Dead Link</a>
          </font>
        </div>
      </div>
    </div>
  </div>
</div>

运行上面的代码片段可以使问题非常清楚。我愿意接受任何建议,谢谢你。

编辑:我错误地删除了我在脚本中留下的错误功能。

0 个答案:

没有答案