我试着把一个div移到左边,然后用javascript触发它,我很确定我把所有的东西都弄对了,但是它不工作。代码如下:
<html>
<head>
<style>
#container {
width: 100%;
height: 500px;
position: relative;
overflow:hidden;
background-color:black;
border-radius:10px;
}
#item {
width: 100px;
height: 100px;
position: absolute;
border-radius:10px;
overflow:hidden;
}
#stars {
width: 200%;
height: 500px;
position: absolute;
background-image: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
display:block;
background-size:contain;
bottom:0;
left:0;
right:0;
z-index:1;
background-repeat:repeat-x;
}
h1 {
font-family: montserrat;
}
@keyframes stars{
100%{transform:translateX(-1200px);}
}
</style>
</head>
<body bgcolor=white>
<h1>Animasi javascript</h1>
<p>
<button onclick="myMove()">Animate</button>
<button onclick="stop()">Stop</button>
</p>
<div id ="container">
<div id="stars"></div>
</div>
<script>
function myMove() {
var stars = document.getElementById=("stars");
stars.style.animation ="stars 10s linear infinite";
}
</script>
</body>
</html>我可以只使用css的动画,但它每次都会运行,我想让它从一个按钮开始。
发布于 2020-10-25 16:24:11
var stars = document.getElementById=("stars"); <-- remove "="
var stars = document.getElementById("stars");
var stars = document.getElementById("stars");
function myMove() {
stars.style.animation ="stars 2s linear infinite";
}
function stop() {
stars.style.animation ="none";
}#container {
width: 100%;
height: 500px;
position: relative;
overflow:hidden;
background-color:black;
border-radius:10px;
}
#item {
width: 100px;
height: 500px;
position: absolute;
border-radius:10px;
overflow:hidden;
}
#stars {
width: 200%;
height: 500px;
position: absolute;
background-image: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%);
display:block;
background-size:contain;
bottom:0;
left:0;
right:0;
z-index:1;
background-repeat:repeat-x;
}
h1 {
font-family: montserrat;
}
@keyframes stars{
100%{transform:translateX(-1200px);}
}<body bgcolor=white>
<h1>Animasi javascript</h1>
<p>
<button onclick="myMove()">Animate</button>
<button onclick="stop()">Stop</button>
</p>
<div id ="container">
<div id="stars"></div>
</div>
</body>
https://stackoverflow.com/questions/64521676
复制相似问题