首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Javascript停止和启动动画?

如何使用Javascript停止和启动动画?
EN

Stack Overflow用户
提问于 2020-05-09 02:55:02
回答 3查看 85关注 0票数 2

我正试着用普通的Javascript来停止这个动画。我曾尝试使用classList.remove属性,但它不起作用。当我打开Chrome Dev工具时,它显示它无法读取属性remove或undefined。我不明白它想说什么。有没有一种方法可以用vanilla JS删除动画,有人可以展示解决方案吗?

代码语言:javascript
复制
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');
})
代码语言:javascript
复制
* {
  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;
  }
}
代码语言:javascript
复制
  <!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>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-05-09 03:18:49

代码语言:javascript
复制
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";
  }
})
代码语言:javascript
复制
* {
  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;
  }
}
代码语言:javascript
复制
<!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>

票数 1
EN

Stack Overflow用户

发布于 2020-05-09 03:06:17

circle变量引用的是旧版本中的HTMLCollectionnodeList。不幸的是,您不能将单个操作应用于vanilla JavaScript中的所有集合。

您可以做的是将集合转换为数组,然后循环遍历它。

代码语言:javascript
复制
const circle = document.getElementsByClassName('circle')
...
const button = document.getElementById('btn')

button.addEventListener('click', function() {
 Array.from(circle).forEach((c) => {
   c.classList.remove('red');
   c.classList.remove('blue');
   c.classList.remove('yellow');
   c.classList.remove('green');
 });
});

请注意,这将完全删除颜色,使灯泡为空。

如果您只需要删除发光动画,则可以使用c.styles.animation = none;

票数 2
EN

Stack Overflow用户

发布于 2020-05-09 03:09:10

将您的addEventListener函数更改为:

代码语言:javascript
复制
button.addEventListener('click', function() {
  Array.prototype.forEach.call(circle, function(el) {
    el.style.animation = "none";
  });
})

https://jsfiddle.net/kv2qc50n/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61686122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档