gpt4 book ai didi

css - 窗体不绕中心轴 (x,y) 旋转

转载 作者:行者123 更新时间:2023-11-28 00:38:10 24 4
gpt4 key购买 nike

即使我在 css 中设置了 [transform-origin: 100% 50%;],与 X 轴和 Y 轴相比,表单不会绕其中心旋转。

问题:如何使表格从中点开始旋转。

    .box {
background-color: pink;
margin: 300px 0 0 300px;
width: 200px;
height: 200px;
}

.box {
position: absolute;
animation: spin 10s;
animation-fill-mode: forwards;
}

.line-horizontal {
background-color: black;
width: 200px;
height: 5px;
margin: 100px 0 0 0;
}

.line-vertical {
background-color: black;
width: 5px;
height: 200px;
margin: -105px 0 0 100px;
}

@keyframes spin {
from {
transform:rotate(0deg);
transform-origin: 100% 50%;
}
to {
transform:rotate(360deg);
}
}
    
<div class="box">
<div class="line-horizontal"></div>
<div class="line-vertical"></div>
</div>

最佳答案

tranform-origin 更改为 50% 50%,这意味着两个轴的中心并且不要将它放在关键帧内,因为它会得到动画

.box {
background-color: pink;
width: 200px;
height: 200px;
}

.box {
position: absolute;
animation: spin 10s;
animation-fill-mode: forwards;
transform-origin: 50% 50%;
/*center*/
}

.line-horizontal {
background-color: black;
width: 200px;
height: 5px;
margin: 100px 0 0 0;
}

.line-vertical {
background-color: black;
width: 5px;
height: 200px;
margin: -105px 0 0 100px;
}

@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
<div class="box">
<div class="line-horizontal"></div>
<div class="line-vertical"></div>
</div>

如果您需要相同的视觉效果,您还可以像下面这样简化代码:

.box {
width: 200px;
height: 200px;
background:
linear-gradient(#000,#000) center/100% 5px,
linear-gradient(#000,#000) center/5px 100%,
pink;
background-repeat:no-repeat;
animation: spin 10s forwards;
transform-origin: 50% 50%; /* OR center*/
}

@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
<div class="box">
</div>

关于css - 窗体不绕中心轴 (x,y) 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54020067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com