我正试着用普通的Javascript来停止这个动画。我曾尝试使用classList.remove属性,但它不起作用。当我打开Chrome Dev工具时,它显示它无法读取属性remove或undefined。我不明白它想说什么。有没有一种方法可以用vanilla 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>
发布于 2020-05-09 03:18:49
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>
发布于 2020-05-09 03:06:17
circle变量引用的是旧版本中的HTMLCollection或nodeList。不幸的是,您不能将单个操作应用于vanilla 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;
发布于 2020-05-09 03:09:10
将您的addEventListener函数更改为:
button.addEventListener('click', function() {
Array.prototype.forEach.call(circle, function(el) {
el.style.animation = "none";
});
})https://stackoverflow.com/questions/61686122
复制相似问题