您需要设置
transform-origin
每个花瓣的中心。这个
transform origin
是每个花瓣将围绕其旋转的点。因此,如果将所有花瓣放置在花朵的边缘,并将变换原点设置为中心,则在应用时
transform: rotate()
对于每个花瓣,它们将围绕花朵的边缘旋转。
.wrap {
width: 100%;
height: 300px;
position: relative;
}
.center {
width: 200px;
height: 200px;
border-radius: 999px;
background-color: rgba(20, 30, 255, 0.5);
position: absolute;
top: 50px;
left: 0;
right: 0;
margin: auto;
}
.petal {
width: 100px;
height: 100px;
border-radius: 999px;
position: absolute;
top: 0;
left: 0;
right: 0;
margin: auto;
background-color: rgba(20, 30, 255, 0.5);
transform-origin: center 150px;
}
.petal:nth-child(1) {
transform: rotate(45deg);
}
.petal:nth-child(2) {
transform: rotate(90deg);
}
.petal:nth-child(3) {
transform: rotate(135deg);
}
.petal:nth-child(4) {
transform: rotate(180deg);
}
.petal:nth-child(5) {
transform: rotate(225deg);
}
.petal:nth-child(6) {
transform: rotate(270deg);
}
.petal:nth-child(7) {
transform: rotate(315deg);
}
.petal:nth-child(8) {
transform: rotate(360deg);
}
<div class="wrap">
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="petal"></div>
<div class="center"></div>
</div>
编辑以添加
执行
变换:旋转()
将旋转花瓣本身以及花瓣元素中的任何内容。如果内容不应该旋转,则必须使用三角法计算每个花瓣的位置,而不是围绕花的中心旋转花瓣。
事实上,8个等距的花瓣很容易做到,因为它们的位置是45°的倍数。
在本例中,花朵的中心位于
top: 50%
和
left: 50%
.花心的半径为
100px
,通过使用勾股定理,我们可以计算
71px
从花的中心向左和向上将是它圆周上45°的点。要放置花瓣的中心,我们还需要考虑花瓣的宽度,即
50px
。
所有花瓣的完整示例如下:
.wrap {
width: 100%;
height: 300px;
position: relative;
}
.center {
width: 200px;
height: 200px;
border-radius: 999px;
background-color: rgba(20, 30, 255, 0.5);
position: absolute;
top: 50px;
left: 0;
right: 0;
margin: auto;
}
.petal {
width: 100px;
height: 100px;
border-radius: 999px;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
background-color: rgba(20, 30, 255, 0.5);
}
.petal:nth-child(1) {
top: calc(50% - 71px - 50px);
left: calc(50% + 71px - 50px);
}
.petal:nth-child(2) {
top: calc(50% - 50px);
left: calc(50% + 100px - 50px);
}
.petal:nth-child(3) {
top: calc(50% + 71px - 50px);
left: calc(50% + 71px - 50px);
}
.petal:nth-child(4) {
top: calc(50% + 100px - 50px);
left: calc(50% - 50px);
}
.petal:nth-child(5) {
top: calc(50% + 71px - 50px);
left: calc(50% - 71px - 50px);
}
.petal:nth-child(6) {
top: calc(50% - 50px);
left: calc(50% - 100px - 50px);
}
.petal:nth-child(7) {
top: calc(50% - 71px - 50px);
left: calc(50% - 71px - 50px);
}
.petal:nth-child(8) {
top: calc(50% - 100px - 50px);
left: calc(50% - 50px);
}
<div class="wrap">
<div class="petal">petal 1</div>
<div class="petal">petal 2</div>
<div class="petal">petal 3</div>
<div class="petal">petal 4</div>
<div class="petal">petal 5</div>
<div class="petal">petal 6</div>
<div class="petal">petal 7</div>
<div class="petal">petal 8</div>
<div class="center"></div>
</div>