我只想通过曲线路径为我的图像制作动画。像这样。 (我使用 position absolute 进行定位。)做了一些研究,发现 css transform 可以完成这项工作。它可以通过直线轻松完成。但是 flex 的路径?
我尝试与 css transform-origin + transform:rotate 结合使用,但我没有得到我想要的确切结果。很明显,我想向左 flex 移动 80% 左右,需要回到原来的位置。我尝试了很多次调整我的代码,但仍然没有运气。
附言
transform-origin 在这里到底做了什么?有必要吗? 有人可以向我解释一下 transform:rotate 在这里是如何工作的吗?
这是我的代码
.sun{
width: 5.7%;
position: absolute;
top: -5%;
left: 57%;
animation: circle 10s linear infinite;
transform-origin: 0px 700px;
animation-fill-mode: forwards;
animation-direction: alternate;
}
@keyframes circle {
from {
transform:rotate(-60deg);
}
to {
transform:rotate(40deg);
}
}
<div class="sun">
<img src="sun.png" alt="">
</div>
最佳答案
也许通过旋转使父元素移动,而子元素(在我的例子中是伪元素,无论如何)使位置相对于父元素是绝对的。并且只使用动画。看看我的解决方案。也许您必须创建一些包装器并使用 overflow: hidden
,因为它是旋转的正方形。您可以通过添加 background-color
来观察 Square 的行为。
@keyframes move-sun {
from {
transform: rotate(0deg);
}
to {
transform: rotate(90deg);
}
}
.sun {
position: relative;
width: 400px;
height: 400px;
margin: 200px;
transform: rotate(90deg);
animation: move-sun 10s;
}
.sun::before {
content: "";
position: absolute;
top: -25px;
left: -25px;
width: 50px;
height: 50px;
background-color: #ff0;
border-radius: 50%;
}
<div class="sun">
</div>
关于html - CSS - flex 路径中的动画对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46949940/