猿问

如何解决双手风琴中的 maxHeight?

我有两个手风琴,一个放在另一个里面:主要的一个是类别,第二个是问题/答案对。

我的问题是,当我单击一个问题时,类别的整体 maxHeight 与以前相同,迫使我的问答对溢出,而不是添加到类别的 maxHeight。我对 Js 很陌生,所以我很难理解我所缺少的东西。我所有摆弄 js 代码的尝试都失败了。

这是我正在处理的代码笔:https://codepen.io/iuliard/pen/KKMzMLe

////ACCORDION////



var accordions1 = document.getElementsByClassName("category_name");


for (var i = 0; i < accordions1.length; i++) {

  accordions1[i].onclick = function() {

    this.classList.toggle('is-open');


    var content1 = this.nextElementSibling;

    if (content1.style.maxHeight) {

      // accordion is currently open, so close it

      content1.style.maxHeight = null;

    } else {

      // accordion is currently closed, so open it

      content1.style.maxHeight = content1.scrollHeight + "px";

    }

  }

}


var accordions2 = document.getElementsByClassName("question");


for (var i = 0; i < accordions2.length; i++) {

  accordions2[i].onclick = function() {

    this.classList.toggle('is-open');


    var content2 = this.nextElementSibling;

    if (content2.style.maxHeight) {

      // accordion is currently open, so close it

      content2.style.maxHeight = null;

    } else {

      // accordion is currently closed, so open it

      content2.style.maxHeight = content2.scrollHeight + "px";

    }

  }

}


慕码人8056858
浏览 94回答 1
1回答

慕斯709654

您必须计算每个打开max-height的。所以我修改了你打开问题的代码。您可以在此问题的末尾查看基于您的代码的工作示例。category_contentquestion我做了什么在设置问题内容的高度之前:content2.style.maxHeight = content2.scrollHeight + "px";将父类别内容放入变量中:const parent = content2.closest(".category_content");获取其高度(我使用 parseInt() 获取“305”而不是“305&nbsp;px&nbsp;”)并将其与问题内容高度相加:const parentHeight = parseInt(parent.style.maxHeight) + content2.scrollHeight;之后应用新高度(父母+孩子身高):parent.style.maxHeight = parentHeight + "px";&nbsp;我们开始吧。这只是向您指出正确解决方案的示例var accordions1 = document.getElementsByClassName("category_name");for (var i = 0; i < accordions1.length; i++) {&nbsp; accordions1[i].onclick = function() {&nbsp; &nbsp; this.classList.toggle('is-open');&nbsp; &nbsp; var content1 = this.nextElementSibling;&nbsp; &nbsp; if (content1.style.maxHeight) {&nbsp; &nbsp; &nbsp; // accordion is currently open, so close it&nbsp; &nbsp; &nbsp; content1.style.maxHeight = null;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; // accordion is currently closed, so open it&nbsp; &nbsp; &nbsp; content1.style.maxHeight = content1.scrollHeight + "px";&nbsp; &nbsp; }&nbsp; }}var accordions2 = document.getElementsByClassName("question");for (var i = 0; i < accordions2.length; i++) {&nbsp; accordions2[i].onclick = function() {&nbsp; &nbsp; this.classList.toggle('is-open');&nbsp; &nbsp; var content2 = this.nextElementSibling;&nbsp; &nbsp; if (content2.style.maxHeight) {&nbsp; &nbsp; &nbsp; // accordion is currently open, so close it&nbsp; &nbsp; &nbsp; content2.style.maxHeight = null;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; // ↓↓↓ this is what I've modified ↓↓↓&nbsp; &nbsp; &nbsp; const parent = content2.closest(".category_content");&nbsp; &nbsp; &nbsp; const parentHeight = parseInt(parent.style.maxHeight) + content2.scrollHeight;&nbsp; &nbsp; &nbsp; parent.style.maxHeight = parentHeight + "px";&nbsp; &nbsp; &nbsp; // accordion is currently closed, so open it&nbsp; &nbsp; &nbsp; content2.style.maxHeight = content2.scrollHeight + "px";&nbsp; &nbsp; }&nbsp; }}@import url("https://rsms.me/inter/inter.css");html {&nbsp; font-family: "Inter", sans-serif;}@supports (font-variation-settings: normal) {&nbsp; html {&nbsp; &nbsp; font-family: "Inter var", sans-serif;&nbsp; }}body {&nbsp; background: orange;}/* ACCORDION&nbsp; */.container {&nbsp; max-width: 100%;&nbsp; padding: 0px 35px;}.category {&nbsp; padding-top: 25px;}/* category name */dl h3 {&nbsp; font-family: "Inter";&nbsp; font-weight: 500;&nbsp; font-size: 18px;&nbsp; text-align: left;&nbsp; text-transform: uppercase;&nbsp; padding-bottom: 20px;&nbsp; border-bottom: 1px solid black;&nbsp; line-height: 1.7;&nbsp; cursor: pointer;&nbsp; transition: 0.2s linear;}dl h3:hover {&nbsp; transform: translateX(3px);}/* question */dt h4 {&nbsp; display: flex;&nbsp; order: 2;&nbsp; max-width: 50%;&nbsp; font-size: 16px;&nbsp; font-weight: 400;&nbsp; line-height: 180%;}/* answer */dd p {&nbsp; font-family: "Inter";&nbsp; font-size: 16px;&nbsp; max-width: 75ch;&nbsp; line-height: 180%;&nbsp; padding-left: 29px;&nbsp; padding-bottom: 20px;&nbsp; font-weight: 400;}a {&nbsp; color: black;}.category_content {&nbsp; background-color: orange;&nbsp; max-height: 0;&nbsp; overflow: hidden;&nbsp; transition: max-height 0.3s ease-in-out;}dd.answer {&nbsp; background-color: orange;&nbsp; max-height: 0;&nbsp; overflow: hidden;&nbsp; transition: max-height 0.3s ease-in-out;&nbsp; border-bottom: 1px dashed black;}dt.question {&nbsp; display: inline-flex;&nbsp; cursor: pointer;&nbsp; padding: 15px 0px;&nbsp; font-family: "Inter";&nbsp; font-weight: 400;&nbsp; font-size: 16px;&nbsp; transition: transform 0.2s linear;&nbsp; line-height: 150%;&nbsp; width: 100%;}/* arrow&nbsp; */dt.question:after {&nbsp; content: "\f055";&nbsp; font-family: "fontawesome";&nbsp; margin-right: 12px;&nbsp; padding-top: 3px;}@-moz-document url-prefix() {&nbsp; dt.question:after {&nbsp; &nbsp; margin-top: 0px;&nbsp; }}dt.question.is-open:after {&nbsp; content: "\f056";}dt.question:hover,dt.question.is-open {&nbsp; transform: translateX(5px);}&nbsp; &nbsp; <!-- accordion starts here -->&nbsp; &nbsp; <div class="container">&nbsp; &nbsp; &nbsp; <dl class="category">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="category_name">Lorem</h3><div class="category_content">&nbsp; &nbsp; &nbsp; &nbsp; <dt class="question"><h4>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium?</h4></dt>&nbsp; &nbsp; &nbsp; &nbsp; <dd class="answer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; </dd>&nbsp; &nbsp; &nbsp; &nbsp; <dt class="question"><h4>Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam?</h4></dt>&nbsp; &nbsp; &nbsp; &nbsp; <dd class="answer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; </dd>&nbsp; &nbsp; &nbsp; &nbsp; <dt class="question"><h4>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</h4></dt>&nbsp; &nbsp; &nbsp; &nbsp; <dd class="answer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; </dd>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </dl>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <dl class="category">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="category_name">Lorem2</h3><div class="category_content">&nbsp; &nbsp; &nbsp; &nbsp; <dt class="question"><h4>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium?</h4></dt>&nbsp; &nbsp; &nbsp; &nbsp; <dd class="answer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim veniam.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; </dd>&nbsp; &nbsp; &nbsp; &nbsp; <dt class="question"><h4>Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam?</h4></dt>&nbsp; &nbsp; &nbsp; &nbsp; <dd class="answer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; </dd>&nbsp; &nbsp; &nbsp; &nbsp; <dt class="question"><h4>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</h4></dt>&nbsp; &nbsp; &nbsp; &nbsp; <dd class="answer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; </dd>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </dl>&nbsp; &nbsp; </div>&nbsp; &nbsp; <!-- accordion ends here -->
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答