2 回答
TA贡献1880条经验 获得超4个赞
我没有使用display block inline,因为我需要打开和关闭时的持续时间。我用简单的 javascript 代码创建了一个非常简单的手风琴,我没有关注 css,因为你可以放置任何你想要的 css,我们的重点是 javascript 想法和简单的 css 代码。height:0; transition: all 1s linear在 css 中使用而不是在 Javascript 中使用,您可以在 javascript 中height: 0使用更改为原始高度scrollHeight,即使您在 css put 中,它也会为您提供元素的主要高度height: 0。如果你有任何问题就问我。如果您愿意,我可以使用与我的代码相同的想法来编辑您的代码。
var opencloseques = document.getElementsByClassName("opencloseques");
for (let i = 0; i < opencloseques.length; i = i + 1) {
opencloseques[i].onclick = function () {
if (this.classList.contains("opened") == false) {
this.parentElement.getElementsByClassName("answer")[0].style.height = this.nextElementSibling.scrollHeight + "px";
this.classList.add("opened")
} else {
this.parentElement.getElementsByClassName("answer")[0].style.height = "0";
this.classList.remove("opened")
}
}
}
.questionanswer .answer {
height: 0;
overflow: hidden;
transition: all 0.3s linear;
}
<div class="all">
<div class="questionanswer">
<button class="opencloseques">Click Me<span class='span'>+</span></button>
<div class="answer">
<p>Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it </p>
</div>
</div>
<div class="questionanswer">
<button class="opencloseques">Click Me<span class='span'>+</span></button>
<div class="answer">
<p>Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it Welcome To my website i hope you enjoy it </p>
</div>
</div>
</div>
添加回答
举报