Apologies in advance, I am completely new to transitions.
I am working with CSS and JavaScript to attempt a menu that slides down smoothly on click, and returns just as smoothly with another click.
I tried to mimic an example animation as shown in here: https://codepen.io/shshaw/pen/gsFch
My relevant code is as follows:
HTML
<div class="sort">
<div class="sort-options">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
</div>
CSS
.sort-options {
margin: 20px;
display: none;
flex-direction: column;
align-items: flex-start;
font-size: 20px;
width: 100%;
transition: display 3s ease-out;
}
JS
const sortDiv = document.querySelector('.sort')
sortDiv.addEventListener('click', function() {
const sortOptions = document.querySelector('.sort-options');
if (sortOptions.style.display === 'none') {
sortOptions.style.display = 'flex';
} else {
sortOptions.style.display = 'none';
}
});
The click does hide and show the element, but no animation is given. I tried to achieve the effect using only CSS, but I have found the same result, in which it does hide and show, but no smooth sliding down. This makes me think I am not doing something right with CSS.
CSS only attempt:
.sort:hover .sort-options {
display: flex;
}
.sort-options {
visibility: hidden;
margin: 20px;
display: none;
flex-direction: column;
align-items: flex-start;
font-size: 20px;
width: 100%;
transition: all 3s ease-in-out;
}
My full code is here if you want to review it: https://wulfenn.github.io/todolist
I did some browsing around stackoverflow and it seems that display is not supported as a transition effect, but I am not certain how to achieve the effect using visibility while not occupying the space if the div is not visible.
Help appreciated!