How to animate when you use classList.remove or classList.Add

Viewed 214

the following code is this.

const showBoxBtn = document.querySelector('.showBox');
const closeBoxBtn = document.querySelector('.closeBox');
const box = document.querySelector('.box');

const showBox = function () {
  box.classList.remove('hidden');
}

const closeBox = function () {
  box.classList.add('hidden')
}

showBoxBtn.addEventListener('click', showBox );
closeBoxBtn.addEventListener('click', closeBox);
.box {
  width: 100px;
  height: 100px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #000;
  display:flex;
  position: absolute;
  animation: slide-in-top 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
}

@keyframes slide-in-top {
  0% {
    transform: translateY(-1000px);

    opacity: 0;
  }
  100% {
    transform: translateY(0), translate(-50%, -50%);
    opacity: 1;
  }
}

.hidden {
  display:none;
}
<button class="showBox">Show</button>
<button class="closeBox">Close</button>

<div class="box hidden"></div>

The problem is the animation don't work on classList.remove. I want to make same animation , or reverse or another.

You can find the JSFiddle here.

I hope you can help me!

Thanks

2 Answers

I added another class to show and hide the box with the existing animation but played in reverse. I added an eventListener for animationend to remove the necessary classes.

Here's your solution:

const showBoxBtn = document.querySelector('.showBox');
const closeBoxBtn = document.querySelector('.closeBox');
const box = document.querySelector('.box');

const showBox = function() {
  box.classList.add('will-show');
  box.classList.remove('hidden');
  box.addEventListener("animationend", function() {
    box.classList.remove('will-show');
    box.classList.add('shown');
    console.log(box.classList.value);
  }, {once: true});
}

const closeBox = function() {
  box.classList.add('will-hide');
  //shown class was removed here
  box.classList.remove('shown');
  box.addEventListener("animationend", function() {
    box.classList.remove('will-hide');
    box.classList.add('hidden');

    //supposed to be box hidden only
    console.log(box.classList.value);
  }, {once: true});
}

showBoxBtn.addEventListener('click', showBox);
closeBoxBtn.addEventListener('click', closeBox);
.box {
  width: 100px;
  height: 100px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #000;
  display: flex;
  position: absolute;
}

@keyframes slide-in-top {
  0% {
    transform: translateY(-1000px);
    opacity: 0;
  }
  100% {
    transform: translateY(0), translate(-50%, -50%);
    opacity: 1;
  }
}

.will-show {
  animation: slide-in-top 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
}

.will-hide {
  animation: slide-in-top 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94) both;
  animation-direction: reverse;
}

.shown {
  display: block;
}

.hidden {
  display: none;
}
<button class="showBox">Show</button>
<button class="closeBox">Close</button>

<div class="box hidden"></div>

You may add one line to each function like below.

const showBox = function () {
  box.classList.remove('hidden');
   box.classList.add('box')
}

const closeBox = function () {
  box.classList.add('hidden')
    box.classList.remove('box')
}

Related