Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not able to repeat CSS class removal function with JavaScript after running once

I am not able to repeat the animation and class modifications, triggered by either of the two buttons after running through one time. e.g. Click "Bottom Base" a first time: square flashes yellow then remains blue. Click "Bottom Base" once more: nothing changes.

Current behavior:

  1. Click "Bottom Base" a first time: bottom square flashes yellow then remains blue.
  2. Click "Bottom Base" once more: nothing changes with bottom square.

Desired behavior:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Click "Bottom Base" a first time: bottom square flashes yellow then remains blue.
  2. Click "Bottom Base" once more: bottom square flashes yellow then remains blue.

Seeking guidance on how to make this work.

https://jsfiddle.net/8ko3tbva/

const BaseHTMLCollection = [document.getElementById("b1"), document.getElementById("b2")];

function clearBase(b) {
  BaseHTMLCollection[b].classList.remove("occupiedBase");
  BaseHTMLCollection[b].classList.remove("animatedBaseHit");
}

function flashBaseColor(b, a) {
  if (a == "H") {
    BaseHTMLCollection[b].classList.add("animatedBaseHit");
  }
}

function updateBaseColor(b, a) {
  BaseHTMLCollection[b].classList.add("occupiedBase");
  if (b == 1) {
    BaseHTMLCollection[b - 1].classList.remove("occupiedBase");
  }
}

function baseAction(base, action) {

  clearBase(base);
  flashBaseColor(base, action);
  updateBaseColor(base, action);

}
#bases {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 20vw;
  width: 20vw;
  margin-top: 5vw;
  margin-right: 5vw;
}

#b1 {
  bottom: 0;
  left: 0;
}

#b2 {
  top: 0;
  left: 0;
}

.base {
  background: rgb(44, 44, 44);
  border-style: solid;
  border-width: thick;
  box-shadow: -8px 8px 20px black;
  width: 42%;
  height: 42%;
  position: absolute;
}

.animatedBaseHit {
  animation: pulseBaseHit 0.8s 3;
}

@keyframes pulseBaseHit {
  0% {
    transform: scale(1.05);
    background: yellow;
  }
  50% {
    transform: scale(0.9);
    background: rgb(44, 44, 44);
    box-shadow: -2px 2px 20px black;
  }
  100% {
    transform: scale(1.05);
    background: yellow;
  }
}

.occupiedBase {
  background: blue;
}
<button type="button" name="bottomBase" onclick="baseAction(0,'H')">Bottom Base</button>
<button type="button" name="topBase" onclick="baseAction(1,'H')">Top Base</button>

<br><br>

<div id="bases">
  <div id="b1" class="base"></div>
  <div id="b2" class="base"></div>
</div>

>Solution :

A solution would be to first remove the animation class before adding it. Delay the new addition slightly with setTimeout.

 BaseHTMLCollection[b].classList.remove("animatedBaseHit");
 setTimeout(()=>{
     BaseHTMLCollection[b].classList.add("animatedBaseHit");
 });
const BaseHTMLCollection = [document.getElementById("b1"), document.getElementById("b2")];

function clearBase(b) {
  BaseHTMLCollection[b].classList.remove("occupiedBase");
  BaseHTMLCollection[b].classList.remove("animatedBaseHit");
}

function flashBaseColor(b, a) {
  if (a == "H") {
  BaseHTMLCollection[b].classList.remove("animatedBaseHit");
  setTimeout(()=>{
     BaseHTMLCollection[b].classList.add("animatedBaseHit");
  });
  }
}

function updateBaseColor(b, a) {
  BaseHTMLCollection[b].classList.add("occupiedBase");
  if (b == 1) {
    BaseHTMLCollection[b - 1].classList.remove("occupiedBase");
  }
}

function baseAction(base, action) {

  clearBase(base);
  flashBaseColor(base, action);
  updateBaseColor(base, action);

}
#bases {
  position: absolute;
  top: 0px;
  left: 0px;
  height: 20vw;
  width: 20vw;
  margin-top: 5vw;
  margin-right: 5vw;
}

#b1 {
  bottom: 0;
  left: 0;
}

#b2 {
  top: 0;
  left: 0;
}

.base {
  background: rgb(44, 44, 44);
  border-style: solid;
  border-width: thick;
  box-shadow: -8px 8px 20px black;
  width: 42%;
  height: 42%;
  position: absolute;
}

.animatedBaseHit {
  animation: pulseBaseHit 0.8s 3;
}

@keyframes pulseBaseHit {
  0% {
    transform: scale(1.05);
    background: yellow;
  }
  50% {
    transform: scale(0.9);
    background: rgb(44, 44, 44);
    box-shadow: -2px 2px 20px black;
  }
  100% {
    transform: scale(1.05);
    background: yellow;
  }
}

.occupiedBase {
  background: blue;
}
<button type="button" name="bottomBase" onclick="baseAction(0,'H')">Bottom Base</button>
<button type="button" name="topBase" onclick="baseAction(1,'H')">Top Base</button>

<br><br>

<div id="bases">
  <div id="b1" class="base"></div>
  <div id="b2" class="base"></div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading