javascript - 使用 CSS 轻松地将不同的动画应用于特定的 div,如果不可能的话,使用 JS?

标签 javascript html css css-animations

假设您在一个页面上有 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>

最佳答案

考虑将标签/输入技巧与 ~ 选择器结合使用,以防您可以自由编辑 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>

关于javascript - 使用 CSS 轻松地将不同的动画应用于特定的 div,如果不可能的话,使用 JS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150788/

相关文章:

html - 使flexbox元素居中和右对齐

html - z-index 属性是如何工作的?

html - safari 中的响应式边距定位问题

javascript - 显示从 x 时间到 x 时间的文本(并且将继续下一个文本),如演示文稿

javascript - 使用下拉选择器更改/覆盖 PHP 变量

jQuery nestedsortable插件默认添加css

jquery - 使用 jQuery 更改背景图像

javascript - json 使用变量进行字符串化

IE 的 JavaScript 条件注释

javascript - jQuery 链接多个自定义函数来触发多个面板