Легко применить различные анимации к конкретному div с помощью CSS и, если не возможно, JS? - PullRequest
0 голосов
/ 29 сентября 2019

Допустим, у вас есть 100 кнопок на странице, и каждая из них должна применять различную анимацию к одному конкретному элементу HTML.Есть ли способ легко определить анимацию для другого элемента с помощью CSS, а если нет, то установить его с помощью JavaScript?

Например,

#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>

Когда кнопка нажата, я хочу, чтобы элемент использовал связанный с ней переход.

Можете ли вы определить переход для другого элемента в CSS:

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

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

ИЛИ

@keyframes #Group2 animation3 {

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

Если CSS не справляется с этим, это выглядит нормально для настройкиэто через 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>

Ответы [ 2 ]

2 голосов
/ 29 сентября 2019

Рассмотрим трюк с меткой / вводом в сочетании с селектором ~, если вы можете редактировать HTML-код

#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>
1 голос
/ 29 сентября 2019

Вы можете использовать CSS varibales , чтобы установить свойство animation и изменить значение, используя 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>
...