【发布时间】:2021-06-11 13:38:08
【问题描述】:
【问题讨论】:
-
带有关键帧的css动画
rotateZ() -
不要告诉我你在 google 上没有发现这个琐碎的动画吗?来吧..请稍微努力..
标签: javascript css animation
【问题讨论】:
rotateZ()
标签: javascript css animation
一个简单的 CSS 示例
div {
height: 100px;
width: 100px;
position: relative;
background: blue;
border-radius: 10px;
}
span {
height: 10px;
width: 50%;
left: 50%;
top: calc(50% - 5px);
position: absolute;
background: white;
border-radius: 10em;
animation-name: spin;
animation-duration: 2000ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
transform-origin: left center;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
<div>
<span></span>
</div>
【讨论】:
实现此目的的一种方法是创建一个使用transform: rotateZ 的旋转关键帧。确保将transform-origin 设置为left center,以便“针”div 围绕其左侧(而不是围绕其中心)旋转。
.needle {
width: 100px;
height: 20px;
background-color: lightgray;
position: absolute;
left: 50%;
top: calc(50% - 10px);
border-radius: 40px;
transform-origin: left center;
animation: rotation 2s linear infinite;
}
@keyframes rotation {
from {
transform: rotateZ(0);
}
to {
transform: rotateZ(360deg);
}
}
.container {
width: 200px;
height: 200px;
background-color: darkblue;
position: relative;
border-radius: 10px;
}
<div class="container">
<div class="needle"></div>
</div>
【讨论】: