两个围绕中心 CSS 动画旋转的 div
Two divs orbiting around a center CSS animation
我试图 "orbit" 两个独立的 div 围绕一个中心做圆周运动,但是我无法让两个 div 在我的 CSS 动画中遵循相同的圆周路径。
.container {
width: 500px;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
}
.box {
background-color: pink;
width: 100px;
height: 100px;
animation: battle 6s linear infinite;
position: absolute;
margin: 10px;
}
@keyframes battle {
from {
transform: rotate(0deg) translateX(150px) rotate(0deg);
}
to {
transform: rotate(360deg) translateX(150px) rotate(-360deg);
}
}
<div class="container">
<div class="box">
</div>
<div class="box">
</div>
</div>
我多年前做过的事情可能与您要找的很接近:
// Base
body {
background: #252525;
}
// Keyframes
@keyframes rotateClockwise {
100% {
transform: rotate(360deg);
}
}
@keyframes rotateCounterClockwise {
100% {
transform: rotate(-360deg);
}
}
// Ring
.ring {
position: relative;
left: 50%;
top: 50px;
margin-left: -100px;
height: 200px;
width: 200px;
border: 10px solid #666;
border-radius: 50%;
}
// Dots
.dot {
position: absolute;
height: 250px;
width: 40px;
top: -25px;
left: 50%;
margin-left: -20px;
&:before {
display: block;
content: '';
height: 40px;
width: 40px;
border-radius: 50%;
box-shadow: 0 2px 3px rgba(0,0,0,.1);
}
}
.dot--one {
animation: rotateClockwise 4s linear infinite;
&:before {
background: #e6a933;
}
}
.dot--two {
animation: rotateCounterClockwise 2s linear infinite;
&:before {
background: #e63348;
}
}
.dot--three {
animation: rotateClockwise 7s linear infinite;
&:before {
background: #70b942;
}
}
.dot--four {
animation: rotateCounterClockwise 12s linear infinite;
&:before {
background: #009ee3;
}
}
<div class="ring">
<div class="dot dot--one"></div>
<div class="dot dot--two"></div>
<div class="dot dot--three"></div>
<div class="dot dot--four"></div>
</div>
https://codepen.io/seanstopnik/pen/93f9cbcbcf9b38684bfc75f38c9c4db3
让您的 parent 元素成为向导;
当目标是围绕一个中心以一致的间距旋转(而不是说 "elliptical orbit" 更像是椭圆形图案)时,最简单的技术是提供 parent设置一致的边界并在其中附加 children 以使用其位置作为其动画路径。目标是提供一种单个元素同步旋转的错觉,而实际上只有一个元素以其默认的 transform-origin
中心旋转,作为其中 children "orbiting" 的引导。
在我们的例子中,我们取了一个 parent,其周长大致等于 "orbit desired" 的大小,我们给它一个 50% 的 border-radius
来创建一个圆。这使得元素之间的距离小于或大于另一个点没有任何意义。我们将其设为 position: relative
元素,以便我们可以将 position: absolute
应用于它的任何 children。在此示例中,我们使用伪元素,但它们可以很容易地成为额外的 DOM 节点元素,例如 divs.
通过将 children 固定到 parent 上的特定点,我们创建了与 parent 的 transform-origin
中心的 X/Y 的相等距离我们希望并应用 rotate
transform
来旋转 parent。但是,如果我们只这样做,那么 children 也将遵循该路径而不是保持垂直(因为它被认为是需要的)所以我们简单地 re-use 将相同的动画应用于 parent 但是在 reverse
中抵消其旋转。结果是 parent 元素在一个方向旋转,而 children 在另一个方向旋转,以创建示例中看到的效果。希望这对您有所帮助!
#rotator {
position: relative;
width: 7rem;
height: 7rem;
animation: rotations 6s linear infinite;
border: 1px orange dashed;
border-radius: 50%;
margin: 3rem;
}
#rotator:before, #rotator:after {
position: absolute;
content: '';
display: block;
height: 3rem;
width: 3rem;
animation: inherit;
animation-direction: reverse;
}
#rotator:before {
background-color: red;
top: -.25rem;
left: -.25rem;
}
#rotator:after {
background-color: green;
bottom: -.25rem;
right: -.25rem;
}
@keyframes rotations {
to { transform: rotate(360deg) }
}
<div id="rotator"></div>
我试图 "orbit" 两个独立的 div 围绕一个中心做圆周运动,但是我无法让两个 div 在我的 CSS 动画中遵循相同的圆周路径。
.container {
width: 500px;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
}
.box {
background-color: pink;
width: 100px;
height: 100px;
animation: battle 6s linear infinite;
position: absolute;
margin: 10px;
}
@keyframes battle {
from {
transform: rotate(0deg) translateX(150px) rotate(0deg);
}
to {
transform: rotate(360deg) translateX(150px) rotate(-360deg);
}
}
<div class="container">
<div class="box">
</div>
<div class="box">
</div>
</div>
我多年前做过的事情可能与您要找的很接近:
// Base
body {
background: #252525;
}
// Keyframes
@keyframes rotateClockwise {
100% {
transform: rotate(360deg);
}
}
@keyframes rotateCounterClockwise {
100% {
transform: rotate(-360deg);
}
}
// Ring
.ring {
position: relative;
left: 50%;
top: 50px;
margin-left: -100px;
height: 200px;
width: 200px;
border: 10px solid #666;
border-radius: 50%;
}
// Dots
.dot {
position: absolute;
height: 250px;
width: 40px;
top: -25px;
left: 50%;
margin-left: -20px;
&:before {
display: block;
content: '';
height: 40px;
width: 40px;
border-radius: 50%;
box-shadow: 0 2px 3px rgba(0,0,0,.1);
}
}
.dot--one {
animation: rotateClockwise 4s linear infinite;
&:before {
background: #e6a933;
}
}
.dot--two {
animation: rotateCounterClockwise 2s linear infinite;
&:before {
background: #e63348;
}
}
.dot--three {
animation: rotateClockwise 7s linear infinite;
&:before {
background: #70b942;
}
}
.dot--four {
animation: rotateCounterClockwise 12s linear infinite;
&:before {
background: #009ee3;
}
}
<div class="ring">
<div class="dot dot--one"></div>
<div class="dot dot--two"></div>
<div class="dot dot--three"></div>
<div class="dot dot--four"></div>
</div>
https://codepen.io/seanstopnik/pen/93f9cbcbcf9b38684bfc75f38c9c4db3
让您的 parent 元素成为向导;
当目标是围绕一个中心以一致的间距旋转(而不是说 "elliptical orbit" 更像是椭圆形图案)时,最简单的技术是提供 parent设置一致的边界并在其中附加 children 以使用其位置作为其动画路径。目标是提供一种单个元素同步旋转的错觉,而实际上只有一个元素以其默认的 transform-origin
中心旋转,作为其中 children "orbiting" 的引导。
在我们的例子中,我们取了一个 parent,其周长大致等于 "orbit desired" 的大小,我们给它一个 50% 的 border-radius
来创建一个圆。这使得元素之间的距离小于或大于另一个点没有任何意义。我们将其设为 position: relative
元素,以便我们可以将 position: absolute
应用于它的任何 children。在此示例中,我们使用伪元素,但它们可以很容易地成为额外的 DOM 节点元素,例如 divs.
通过将 children 固定到 parent 上的特定点,我们创建了与 parent 的 transform-origin
中心的 X/Y 的相等距离我们希望并应用 rotate
transform
来旋转 parent。但是,如果我们只这样做,那么 children 也将遵循该路径而不是保持垂直(因为它被认为是需要的)所以我们简单地 re-use 将相同的动画应用于 parent 但是在 reverse
中抵消其旋转。结果是 parent 元素在一个方向旋转,而 children 在另一个方向旋转,以创建示例中看到的效果。希望这对您有所帮助!
#rotator {
position: relative;
width: 7rem;
height: 7rem;
animation: rotations 6s linear infinite;
border: 1px orange dashed;
border-radius: 50%;
margin: 3rem;
}
#rotator:before, #rotator:after {
position: absolute;
content: '';
display: block;
height: 3rem;
width: 3rem;
animation: inherit;
animation-direction: reverse;
}
#rotator:before {
background-color: red;
top: -.25rem;
left: -.25rem;
}
#rotator:after {
background-color: green;
bottom: -.25rem;
right: -.25rem;
}
@keyframes rotations {
to { transform: rotate(360deg) }
}
<div id="rotator"></div>