Using HTML and JavaScript I am trying to create a list that transitions into view when a button is clicked, including a scrollable inner container, using a max-height transition on the parent DIV.
However, while the parent div transitions smoothly, the content instantly appears after the transition completes. It seems to occur when the inner div is overflowing.
Here is an example of what I have been attempting.
let visible = false;
function showHideList() {
const listContainerRef = document.getElementById('list-id');
if (!listContainerRef?.style?.maxHeight)
return;
listContainerRef.style.maxHeight = visible ? 0 + 'px' : 200 + 'px';
visible = !visible;
}
<button onclick="showHideList()" style="padding: 1rem; background: #2aabd2">Show / Hide</button>
<div id="list-id" style="transition: max-height 2s linear; overflow: hidden; max-height: 0;">
<div style="overflow: auto; max-height: 200px">
<div style="background-color: green">
<span style="display: block; background-color: red; padding: 0.5rem 0.75rem">Item 1</span>
<span style="display: block; background-color: red; padding: 0.5rem 0.75rem">Item 2</span>
<span style="display: block; background-color: red; padding: 0.5rem 0.75rem">Item 3</span>
<span style="display: block; background-color: red; padding: 0.5rem 0.75rem">Item 4</span>
<span style="display: block; background-color: red; padding: 0.5rem 0.75rem">Item 5</span>
<span style="display: block; background-color: red; padding: 0.5rem 0.75rem">Item 6</span>
</div>
</div>
</div>