You can give this a style and then transition to the new style when you add a class. Here for example I transition the height and font size with css.
function toggle_div_fun(event) {
let animateMe = event.currentTarget.dataset.animatetarget;
let divelement = document.getElementById(animateMe);
const isfoo = divelement.classList.contains("foo");
// console.log(isfoo);
divelement.classList.toggle("addedclass", isfoo);
divelement.classList.toggle("foo", !isfoo);
}
let el = document.querySelector('.eco-btn')
.addEventListener("click", toggle_div_fun);
.fun-target {
overflow: hidden;
background-color: #0000ff;
color: #fff;
display: flex;
height: 0;
padding: 0 2em;
width: 100vw;
}
.fun-target {
-webkit-transition: all .5s ease;
-moz-transition: all .5s ease;
transition: all 2s ease;
}
.fun-target.addedclass {
height: auto;
padding: 2em 1em;
font-size: 3em;
background-color: #DDffff;
color: #0000FF;
}
<button class="eco-btn" data-animatetarget='sectiontohide'>Click here</button>
<div id="sectiontohide" class="fun-target foo">This is the div to hide</div>