I've got a combination of css and javascript that allows users to expand a particular div from 0 to its initial height when the user clicks on the header of that div.
HTML
<button class="accordion accordion1">
<div class="innerbutton">
<div class="heading">Click to expand me!</div>
</div>
</button>
<div class="panel panelmargin">
Some content that will appear when button is clicked
</div>
CSS
button.accordion.active:after {
content: "\2212";
}
div.panel {
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
Javascript
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight){
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
}
}
I've got a few panels that have content that is about 4000px in height. Upon initial click of the button, scrollHeight is only being set to approx 2400px. When clicking the button several times, the scrollHeight will be correctly set to 4000px and I can see all of my content.
Do you know why the scrollHeight is being set to an incorrect value?
Here is the codepen. I don't have the content that extends to 4000px, but this might give you a better idea of how it functions.
https://codepen.io/anon/pen/QBONoO
Thanks!
?