简体   繁体   English

我不知道为什么这个动画有效

[英]I don't know why this animation is working

if "position:absolute" not exist in #box why animation will not work?如果#box 中不存在“position:absolute”,为什么动画不起作用?

I tried this code when "position:absolute" delete it in #box, but It was not working.当“位置:绝对”在#box 中删除它时,我尝试了此代码,但它不起作用。

<style>
    #box{
        **position:absolute;**
        width:200px;
        height:200px;
        background-color:red;
        animation:animate 2s none infinite alternate;
    }

    @keyframes animate{
        from{
            left:0;
        }
        50%{
            left:500px;
        }
        to{
            left:500px;
        }

    }
</style>

You shouldn't animate left/right/top/bottom, it is better practice to use transforms .您不应该为左/右/上/下设置动画,最好使用transforms Transforms allow the element to visually move, while their space on the DOM remains, making for safer/smoother animation.变换允许元素在视觉上移动,而它们在 DOM 上的空间仍然存在,从而使动画更安全/更流畅。

Try this instead试试这个

#box {
  width: 200px;
  height: 200px;
  background-color: red;
  animation: animate 2s linear forwards infinite alternate;
}

@keyframes animate {
  0% {
    transform: translateX(0px);
  }
  100% {
    transform: translateX(100px);
  }
}

working demo jsfiddle工作演示jsfiddle

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

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