如何使用Java脚本停止和启动动画?

尔凡·埃扎尼(Irfan Ezani)

我试图通过使用香草Javascript来停止该动画。我尝试使用classList.remove属性,但是没有用。当我打开Chrome Dev Tool时,它说它无法读取未定义的属性remove。我不明白它要说的是什么。有没有办法用香草JS删除动画,有人可以显示解决方案吗?

const circle = document.getElementsByClassName('circle')
const red = document.getElementsByClassName('red')
const blue = document.getElementsByClassName('blue')
const yellow = document.getElementsByClassName('yellow')
const green = document.getElementsByClassName('green')
const button = document.getElementById('btn')

button.addEventListener('click', function() {
 circle.classList.remove('red');
 circle.classList.remove('blue');
 circle.classList.remove('yellow');
 circle.classList.remove('green');
})
* {
  box-sizing: border-box;

}

body {
  background: rgb(25, 21, 26);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
  overflow: hidden;
}
.utilities {
  position: absolute;
  top : 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 1rem;
  display: flex;
  justify-content: center;
  align-items: center;
}

.utilities button {
  height: 50px;
  width: 100px;
  background: none;
  color: white;
  outline: none;
  border: 2px solid rgb(184, 134, 222);
  border-radius: 25px;
}

button:hover {
  background: rgb(184, 134, 222);
  cursor: pointer;
  transition: 0.5s ease;
}

.main {
  background:rgb(57, 53, 75);
  border-radius: 25px;
  height: 20vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}

 .circle {
  display: flex;
  margin: 1rem;
  border-radius: 50%;
  height: 50px;
  width: 50px;
  background: rgba(0,0,0,0.3);
  position: relative;
  transition: 1s all ease;
}

.circle:before {
  position: absolute;
  content: '';
  height: 15px;
  width: 15px;
  left: 17.5px;
  top: -15px;
  margin: 0;
  padding: 0;
  display: block;
  background: rgb(68, 53, 73);
  border-radius: 2px;
  display:inline-block;
  border-bottom: 2px solid rgb(97, 81, 107);
}

.circle:after{
  position: absolute;
  content: "";
  top: -20px;
  left: 30px;
  position: absolute;
  width: 70px;
  height: 18.6666666667px;
  border-bottom: solid #222 2px;
  border-radius: 50%;
}

.circle:last-child::after{
  content: '';
  position: absolute;
  border: none;
}

.red {
	background-color: #c0392b;
  animation: glow-1 2s infinite;
}


.yellow {
	background-color: #f1c40f;
  animation: glow-2 2s infinite;
}

.blue {
	background-color: #64fcfe;
 animation: glow-3  2s infinite;
}

.green {
	background-color: #2ecc71;
  animation: glow-4 2s infinite;
}

@keyframes glow-1 {
  0%, 100% {
	box-shadow: 0 0 20px 5px #c0392b;
  }

  50% {
    box-shadow: none;
  }
}

@keyframes glow-2 {
  0%, 100% {
    box-shadow: none;
  }

  50% {
    box-shadow: 0 0 20px 5px #f1c40f;
  }
}

@keyframes glow-3 {
  0%, 100% {
    box-shadow: 0 0 20px 5px #74f7e1;
  }

  50% {
    box-shadow: none;
  }
}

@keyframes glow-4 {
  0%, 100% {
	box-shadow: none;
  }

  50% {
    box-shadow: 0 0 20px 5px #2ecc71;
  }
}
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>RayaLights</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <div class="main">
      <div  class="circle red"></div>
      <div  class="circle yellow"></div>
      <div  class="circle blue"></div>
      <div  class="circle green"></div>
      <div  class="circle red"></div>
      <div  class="circle yellow"></div>
      <div  class="circle blue"></div>
      <div  class="circle green"></div>
    </div>
    <div class="utilities">
      <button id="btn">Stop</button>
    </div>


    <script src="app.js" charset="utf-8"></script>
  </body>
  </html>

h3t1

const circle = document.getElementsByClassName('circle')
const pause = document.getElementById('pause')
const play = document.getElementById('play')
const stop = document.getElementById('stop')
var len = circle.length;

pause.addEventListener('click', function() {
  for (var i = 0; i < len; i++) {
    circle[i].style.animationPlayState = "paused";
    circle[i].style.WebkitAnimationPlayState = "paused";
  }
})
play.addEventListener('click', function() {
  for (var i = 0; i < len; i++) {
    circle[i].removeAttribute("style");
    circle[i].style.animationPlayState = "running";
    circle[i].style.WebkitAnimationPlayState = "running";
  }
})
stop.addEventListener('click', function() {
  for (var i = 0; i < len; i++) {
    circle[i].style.animation = "none";
  }
})
* {
  box-sizing: border-box;
}

body {
  background: rgb(25, 21, 26);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
  overflow: hidden;
}

.utilities {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 1rem;
  display: flex;
  justify-content: center;
  align-items: center;
}

.utilities button {
  height: 50px;
  width: 100px;
  background: none;
  color: white;
  outline: none;
  border: 2px solid rgb(184, 134, 222);
  border-radius: 25px;
  margin: 0 12px;
}

button:hover {
  background: rgb(184, 134, 222);
  cursor: pointer;
  transition: 0.5s ease;
}

.main {
  background: rgb(57, 53, 75);
  border-radius: 25px;
  height: 20vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
}

.circle {
  display: flex;
  margin: 1rem;
  border-radius: 50%;
  height: 50px;
  width: 50px;
  background: rgba(0, 0, 0, 0.3);
  position: relative;
  transition: 1s all ease;
}

.circle:before {
  position: absolute;
  content: '';
  height: 15px;
  width: 15px;
  left: 17.5px;
  top: -15px;
  margin: 0;
  padding: 0;
  display: block;
  background: rgb(68, 53, 73);
  border-radius: 2px;
  display: inline-block;
  border-bottom: 2px solid rgb(97, 81, 107);
}

.circle:after {
  position: absolute;
  content: "";
  top: -20px;
  left: 30px;
  position: absolute;
  width: 70px;
  height: 18.6666666667px;
  border-bottom: solid #222 2px;
  border-radius: 50%;
}

.circle:last-child::after {
  content: '';
  position: absolute;
  border: none;
}

.red {
  background-color: #c0392b;
  animation: glow-1 2s infinite;
}

.yellow {
  background-color: #f1c40f;
  animation: glow-2 2s infinite;
}

.blue {
  background-color: #64fcfe;
  animation: glow-3 2s infinite;
}

.green {
  background-color: #2ecc71;
  animation: glow-4 2s infinite;
}

@keyframes glow-1 {
  0%,
  100% {
    box-shadow: 0 0 20px 5px #c0392b;
  }
  50% {
    box-shadow: none;
  }
}

@keyframes glow-2 {
  0%,
  100% {
    box-shadow: none;
  }
  50% {
    box-shadow: 0 0 20px 5px #f1c40f;
  }
}

@keyframes glow-3 {
  0%,
  100% {
    box-shadow: 0 0 20px 5px #74f7e1;
  }
  50% {
    box-shadow: none;
  }
}

@keyframes glow-4 {
  0%,
  100% {
    box-shadow: none;
  }
  50% {
    box-shadow: 0 0 20px 5px #2ecc71;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>RayaLights</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="main">
    <div class="circle red"></div>
    <div class="circle yellow"></div>
    <div class="circle blue"></div>
    <div class="circle green"></div>
    <div class="circle red"></div>
    <div class="circle yellow"></div>
    <div class="circle blue"></div>
    <div class="circle green"></div>
  </div>
  <div class="utilities">
    <button id="pause">Pause</button>
    <button id="play">Play</button>
    <button id="stop">Stop</button>
  </div>

</body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何停止并重新启动动画gif

来自分类Dev

如何使用“ caliburn micro 2”和“ Windows Phone 8.1”启动动画

来自分类Dev

使用Babylon.JS在循环内停止/重新启动动画

来自分类Dev

Unity C#,如何从另一个脚本调用函数以启动动画?

来自分类Dev

如何使用css制作带有启动动画的动画进度条

来自分类Dev

使用ANT脚本启动和停止tomcat?

来自分类Dev

Matplotlib:单击启动动画的按钮后如何保存动画?

来自分类Dev

如何更改Android –启动徽标(不启动动画)

来自分类Dev

如何更改Android –启动徽标(不启动动画)

来自分类Dev

如何使用我自己的自定义启动动画来编译AOSP项目?

来自分类Dev

如何使用我自己的自定义启动动画来编译AOSP项目?

来自分类Dev

使用jQuery启动和停止CSS3动画

来自分类Dev

在CSS中悬停时如何停止自动滚动动画?

来自分类Dev

如何删除创建的子代,然后重新启动动画

来自分类Dev

如何在每次点击时启动动画

来自分类Dev

如何在 Tkinter 中为事件启动动画循环?

来自分类Dev

停止窗口滚动动画

来自分类Dev

Android应用启动动画

来自分类Dev

XAML从按钮启动动画

来自分类Dev

如何使用Ant脚本停止和重新启动JBoss 7.1 Application Server?

来自分类Dev

使用Ionic和Google Maps,如何为地图标记移动动画到新位置?

来自分类Dev

如何编写将执行xampp启动和停止的脚本

来自分类Dev

如何在bash脚本中启动和停止systemctl服务?

来自分类Dev

如何在nodejs中自动启动和停止python脚本?

来自分类Dev

倒数后如何启动鼠标移动动作触发脚本?

来自分类Dev

使用bash或python脚本启动和停止GCE实例

来自分类Dev

使用python wrapper脚本启动和停止新贵的celery过程

来自分类Dev

Java如何停止程序中先前启动的Shell脚本

来自分类Dev

如何使用Java和远程karaf容器以编程方式部署,启动和停止OSGI捆绑包?

Related 相关文章

  1. 1

    如何停止并重新启动动画gif

  2. 2

    如何使用“ caliburn micro 2”和“ Windows Phone 8.1”启动动画

  3. 3

    使用Babylon.JS在循环内停止/重新启动动画

  4. 4

    Unity C#,如何从另一个脚本调用函数以启动动画?

  5. 5

    如何使用css制作带有启动动画的动画进度条

  6. 6

    使用ANT脚本启动和停止tomcat?

  7. 7

    Matplotlib:单击启动动画的按钮后如何保存动画?

  8. 8

    如何更改Android –启动徽标(不启动动画)

  9. 9

    如何更改Android –启动徽标(不启动动画)

  10. 10

    如何使用我自己的自定义启动动画来编译AOSP项目?

  11. 11

    如何使用我自己的自定义启动动画来编译AOSP项目?

  12. 12

    使用jQuery启动和停止CSS3动画

  13. 13

    在CSS中悬停时如何停止自动滚动动画?

  14. 14

    如何删除创建的子代,然后重新启动动画

  15. 15

    如何在每次点击时启动动画

  16. 16

    如何在 Tkinter 中为事件启动动画循环?

  17. 17

    停止窗口滚动动画

  18. 18

    Android应用启动动画

  19. 19

    XAML从按钮启动动画

  20. 20

    如何使用Ant脚本停止和重新启动JBoss 7.1 Application Server?

  21. 21

    使用Ionic和Google Maps,如何为地图标记移动动画到新位置?

  22. 22

    如何编写将执行xampp启动和停止的脚本

  23. 23

    如何在bash脚本中启动和停止systemctl服务?

  24. 24

    如何在nodejs中自动启动和停止python脚本?

  25. 25

    倒数后如何启动鼠标移动动作触发脚本?

  26. 26

    使用bash或python脚本启动和停止GCE实例

  27. 27

    使用python wrapper脚本启动和停止新贵的celery过程

  28. 28

    Java如何停止程序中先前启动的Shell脚本

  29. 29

    如何使用Java和远程karaf容器以编程方式部署,启动和停止OSGI捆绑包?

热门标签

归档