繁体   English   中英

我想用@keyframes 将 animation 添加到 div 中。 我基本上已经尝试了我所知道的一切

[英]i want to add an animation to the div with @keyframes. I have tried basically everything i know

var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.moveUp{background-color:red; width:40px;height:40px;margin:auto;margin-top:3em;}'
var moveUp = document.querySelectorAll('.moveUp')
document.getElementsByTagName('head')[0].appendChild(style)


//HTML

 <body>
    <div class="moveUp"></div>
    <script src="script.js"></script>
  </body>

我希望 div 向下移动 200px 他们再次出现

您可以将 style.innerHTML 更改为以下内容:

style.innerHTML = '.moveUp{background-color:red; width:40px;height:40px;margin:auto;margin-top:3em;animation: moveDown 1s;}@keyframes moveDown{50% {transform: translateY(200px);}100%{transform: translateY(0);}}'

为了更容易阅读:

.moveUp {
  background-color: red; 
  width: 40px;
  height: 40px;
  margin: auto;
  margin-top: 3em;
  animation: moveDown 1s;
}

@keyframes moveDown {
  50% {
    transform: translateY(200px);
  }
  100%{
    transform: translateY(0);
  }
}

 var style = document.createElement('style'); style.type = 'text/css'; style.innerHTML = '.moveUp{background-color:red; width:40px;height:40px;margin:auto;margin-top:3em;animation: moveDown 1s;}@keyframes moveDown{50% {transform: translateY(200px);}100%{transform: translateY(0);}}' var moveUp = document.querySelectorAll('.moveUp') document.getElementsByTagName('head')[0].appendChild(style)
 <div class="moveUp"></div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM