1

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;
                    }
    });
         
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
easyrlj
  • 99
  • 3
  • 8

3 Answers3

1

All your javascript code with a counter can be replaced with the toggle() method:

divText.classList.toggle('open');

Very important. With the display: none rule you won't be able to make an transition effect. But this can be fixed by using only the height and overflow rules.

let clickBtn = document.querySelector('.myBtn');
let divText = document.querySelector('.myDiv');
    
clickBtn.addEventListener('click', function(event){
  divText.classList.toggle('open');
})
.myDiv{
  background: red;
  width: 60px;
  height: 0;
  transition: height 2s;
  overflow: hidden;
}

.myDiv.open{
  height: 200px;
}
<div><button class="myBtn">Click me</button></div>

<div class="myDiv">
    this is the text.
</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
0

Animation To or From state display:none is not possible. You can animate from height:0; to let say height:200px;

This Topic is alredy discussed mutliple times, like: CSS Animation and Display None

Zeljko
  • 1
  • 3
0

try this

            <style>
        .myDiv {
            transition: opacity 2s ease-out;
            opacity: 0;
            height: 0;
            overflow: hidden;
        }

        .myDiv.open {
            background: red;
            display: inline-block;
            width: 60px;
            height: 200px;
            opacity: 1;
        }
    </style>

    <div><button class="myBtn">Click me</button></div>

    <div class="myDiv">
        this is the text.
    </div>


    <script>
        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;
            }
        });

    </script>