首页 > 解决方案 > 具有多个属性的 CSS 关键帧动画

问题描述

我有一个容器 div,里面有一个对象。我可以为它的一个属性设置动画,例如底部或左侧,但不能同时为两者设置动画。如何同时为两个属性设置动画,使其沿对角线移动?我不明白为什么以下代码不起作用:

#container {
    position: relative;
}

@keyframes move { 
    0%     { left:   0px; bottom:   0px; }
    100%   { left: 122px; bottom: 157px; }
}

#object {
    position: absolute;
    width: 10px; 
    left:   0px;
    bottom: 0px;
    /*animation: name duration timing-function delay iteration-count direction fill-mode play-state; */    
    animation: move 2.5s linear 0s infinite;
}

标签: csscss-animationscakeyframeanimation

解决方案


确实 沿对角线移动:

#container {
    position: relative;
    height: 180px;
}

@keyframes move { 
    0%     { left:   0px; bottom:   0px; }
    100%   { left: 122px; bottom: 157px; }
}

#object {
    position: absolute;
    width: 10px; 
    left:   0px;
    bottom: 0px;
    animation: move 2.5s linear 0s infinite;
}
<div id="container"><div id="object">MOVING</div></div>


推荐阅读