Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
239 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...