1.21 gigawatts
1.21 gigawatts

Reputation: 17756

Easily apply a different animations to a specific div using CSS and if not possible JS?

Let's say you have 100 buttons on a page and each one should apply a different animation to one specific HTML element. Is there a way to easily define an animation for another element using CSS and if not then setting it via JavaScript?

For example,

#group1 {
  display: flex;
  flex-direction: row;
}

#group2 {
  width: 100px;
  height: 100px;
  background-color: gray;
  animation: animation1 1s ease-out;
}

@keyframes animation1 {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes animation2 {
  0% {
    opacity: 0;
    background-color: red;
  }
  100% {
    opacity: 1;
    background-color: blue;
  }
}

@keyframes animation3 {
  0% {
    opacity: 0;
    background-color: purple;
  }
  100% {
    opacity: 1;
    background-color: orange;
  }
}
<div id="group1">
  <button>Animation 1</button>
  <button>Animation 2</button>
  <button>Animation 3</button>
</div>

<div id="group2">

</div>

When the button is pressed I want the element to use the transition related to it.

Can you define a transition for another element in CSS:

#group2 {
   width:100px;
   height:100px;
   background-color: gray;
}

#button2 {
    animation-target: #group2 animation2 1s ease-out;
}

OR

@keyframes #Group2 animation3 {

  0% {
    opacity: 0;
    background-color: purple;
  }
  100% {
    opacity: 1;
    background-color: orange;
  }

If CSS doesn't handle this, does this look OK for setting it via JavaScript:

function changeAnimation(event) {
  var target = event.currentTarget;
  var animation = getComputedStyle(target).getPropertyValue("--animation");
  var group2 = document.getElementById("group2");
  group2.style.setProperty("animation", animation);
  console.log("animation:" + animation);
}

function hide() {
  var group2 = document.getElementById("group2");
  group2.style.setProperty("display", "none");
}

function show() {
  var group2 = document.getElementById("group2");
  group2.style.setProperty("display", "block");
}
#group1 {
  display: flex;
  flex-direction: row;
}

#group2 {
  width: 100px;
  height: 100px;
  background-color: gray;
  animation: animation1 1s ease-out;
}

#button1 {
  --animation: animation1 1s ease-out;
}

#button2 {
  --animation: animation2 1s ease-out;
}

#button3 {
  --animation: animation3 1s ease-out;
}

@keyframes animation1 {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes animation2 {
  0% {
    opacity: 0;
    background-color: red;
  }
  100% {
    opacity: 1;
    background-color: blue;
  }
}

@keyframes animation3 {
  0% {
    opacity: 0;
    background-color: purple;
  }
  100% {
    opacity: 1;
    background-color: orange;
  }
}
<div id="group1">
  <button id="button1" onclick="changeAnimation(event)">Animation 1</button>
  <button id="button2" onclick="changeAnimation(event)">Animation 2</button>
  <button id="button3" onclick="changeAnimation(event)">Animation 3</button>
  <button id="hide" onclick="hide()">Hide</button>
  <button id="hide" onclick="show()">Show</button>
</div>

<div id="group2">

</div>

Upvotes: 0

Views: 61

Answers (2)

Temani Afif
Temani Afif

Reputation: 272817

Consider the label/input trick combined with the ~ selector in case you are free to edit the HTML code

#group1 {
  display: flex;
  flex-direction: row;
}

#group2 {
  width: 100px;
  height: 100px;
  background-color: gray;
  animation:none  1s ease-out
}
#anim1:checked ~ #group2  {
  animation-name: animation1;
}
#anim2:checked ~ #group2  {
  animation-name: animation2;
}
#anim3:checked ~ #group2  {
  animation-name: animation3;
}

label {
 -webkit-appearance:button;
 -moz-appearance:button;
 padding:5px;
}
input{
 display:none;
}

@keyframes animation1 {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes animation2 {
  0% {
    opacity: 0;
    background-color: red;
  }
  100% {
    opacity: 1;
    background-color: blue;
  }
}

@keyframes animation3 {
  0% {
    opacity: 0;
    background-color: purple;
  }
  100% {
    opacity: 1;
    background-color: orange;
  }
}
<div id="group1">
  <label for="anim1" >Animation 1</label>
  <label for="anim2" >Animation 2</label>
  <label for="anim3" >Animation 3</label>
</div>
<input id="anim1" type="radio" name="anim">
<input id="anim2" type="radio" name="anim">
<input id="anim3" type="radio" name="anim">
<div id="group2">

</div>

Upvotes: 2

Nidhin Joseph
Nidhin Joseph

Reputation: 10237

You can use CSS varibales to set the animation property and change the value using setProperty()

var group2 = document.getElementById('group2');

function changeAnimation(e) {
  group2.style.setProperty('--anim', `animation${e.id}`);
}
:root {
  --anim: animation1;
}

#group1 {
  display: flex;
  flex-direction: row;
}

#group2 {
  width: 100px;
  height: 100px;
  background-color: gray;
  animation: var(--anim) 1s ease-out;
}

@keyframes animation1 {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@keyframes animation2 {
  0% {
    opacity: 0;
    background-color: red;
  }
  100% {
    opacity: 1;
    background-color: blue;
  }
}

@keyframes animation3 {
  0% {
    opacity: 0;
    background-color: purple;
  }
  100% {
    opacity: 1;
    background-color: orange;
  }
}
<div id="group1">
  <button id="1" onclick="changeAnimation(this)">Animation 1</button>
  <button id="2" onclick="changeAnimation(this)">Animation 2</button>
  <button id="3" onclick="changeAnimation(this)">Animation 3</button>
</div>

<div id="group2">

</div>

Upvotes: 1

Related Questions