I have a very simple Javascript code, and CSS.
I have the CSS of myDiv in a display: none status.
When I add the Javascript and run the code the MyDiv
is popping open and the animation is not visible, when I put a height on the div even 0, and click the button it is opening with (transition) animation.
Why is this and is it possible to animate from display: none
?
.myDiv{
display: none;
}
.myDiv.open{
background: red;
display: inline-block;
width: 60px;
height:200px;
transition: height 2s;
}
<div><button class="myBtn">Click me</button></div>
<div class="myDiv">
this is the text.
</div>
let clickBtn = document.querySelector('.myBtn');
let divText = document.querySelector('.myDiv');
let statusclose = true;
clickBtn.addEventListener('click', function(event){
if(statusclose == true){
divText.classList.add('open');
return statusclose = false;
}else if(statusclose == false){
divText.classList.remove('open');
return statusclose = true;
}
});