欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

防止第一個折疊項在頁面加載JS上展開

黃文隆1年前9瀏覽0評論

我有一個使用純javascript開發(fā)的簡單的手風琴,沒有jQuery和純CSS,一切都很好,問題是第一個手風琴在頁面加載時擴展了,我嘗試了許多使用CSS和JS的方法,但都不起作用,我不擅長JavaScript,我試圖在窗口加載時刪除活動類,它刪除了,但它仍然擴展!!你能幫忙嗎?

// ============== toggle accordion =================//
let header = document.querySelectorAll(".accordion-header");



// ============= get all accoridon header =============//
header.forEach(
  (header) => {
    header.addEventListener("click", function(e) {
      let accordion = document.querySelectorAll(".accordion");
      let parentElm = header.parentElement;
      let siblings = this.nextElementSibling;



      // ============= remove accordion body height ==========//
      accordion.forEach((element) => {
        element.children[1].style.maxHeight = null;
      });

      // =========== toggle active class ==============//
      parentElm.classList.toggle("active");
      if (parentElm.classList.contains("active")) {
        // ============== remove active class from all the accordions ===//
        accordion.forEach((element) => {
          element.classList.remove("active");
        });
        // ============== toggle active class where we clicked ========//

        parentElm.classList.toggle("active");
        // ============= set max height ============//
        if (siblings.style.maxHeight) {
          siblings.style.maxHeight = null;
        } else {
          siblings.style.maxHeight = siblings.scrollHeight + "px";
        }
      }
    });
  }
);

window.onload = function() {
  header[0].click();
}

.accordion-container {
  padding: 0 100px;
}

.accordion .accordion-header {
  padding: 15px 20px;
  cursor: pointer;
  position: relative;
}

.accordion .accordion-header h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 400;
  line-height: 32px;
  text-decoration: underline;
}

.accordion .accordion-body {
  max-height: 0;
  overflow: hidden;
  transition: max-height ease 0.5s;
  padding: 0 20px;
}

.accordion .accordion-body p {
  font-weight: 400;
  padding-bottom: 20px;
  line-height: 1.5;
}

<div class="accordion">
  <div class="accordion-header">
    <h2>Accordion Header</h2>
  </div>
  
  <div class="accordion-body">
    <p>Accordion Body</p>
  </div>
</div>

<div class="accordion">
  <div class="accordion-header">
    <h2>Accordion Header</h2>
  </div>
  
  <div class="accordion-body">
    <p>
      <p>Accordion Body</p>
    </p>
  </div>
</div>

下面的代碼將觸發(fā)點擊第一個標題。這將導致手風琴展開。只要刪除這段代碼,它應該會停止自動擴展。

window.onload = function() {
  header[0].click();
}