首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用javascript触发css动画

问无法使用javascript触发css动画
EN

Stack Overflow用户
提问于 2020-10-25 16:16:14
回答 1查看 25关注 0票数 0

我试着把一个div移到左边,然后用javascript触发它,我很确定我把所有的东西都弄对了,但是它不工作。代码如下:

代码语言: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的动画,但它每次都会运行,我想让它从一个按钮开始。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-25 16:24:11

代码语言:javascript
复制
var stars = document.getElementById=("stars"); <-- remove "="
var stars = document.getElementById("stars");

​

代码语言:javascript
复制
var stars = document.getElementById("stars");
  
function myMove() {
  stars.style.animation ="stars 2s linear infinite";
}

function stop() {
  stars.style.animation ="none";
}
代码语言:javascript
复制
#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);}
}
代码语言:javascript
复制
<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>

​

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

https://stackoverflow.com/questions/64521676

复制
相关文章

相似问题

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