两个div绕着中心旋转的CSS动画

3
我正在尝试让两个独立的div绕着中心以圆形运动的方式“轨道运行”,但是我在CSS动画中遇到了两个div无法沿着相同圆形路径移动的问题。

.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>

Jsfiddle


2
难道不是因为它们都是绝对定位吗?它们在旋转,但是它们重叠在一起。试着移动一个像素看看是否有帮助。 - Tomek Buszewski
您能详细解释一下您想要什么吗?您希望div彼此对齐吗?您希望它们平行吗? - Libra
它们已经遵循相同的路径,只是重叠了。 - DigitalJedi
2个回答

11

让你的父元素成为指导;

当目标是围绕中心以一致的间距旋转(而不是像“椭圆轨道”那样更像椭圆形的图案)时,最简单的技术是提供一个父元素来设置一致的边界,并在其中附加子元素,以使用其位置作为它们的动画路径。目标只是提供一种看似各个元素同步运行的幻象,而实际上只有一个元素以其默认的transform-origin中心作为指导,其余子元素“在其中运行”。

在我们的例子中,我们选择了一个等周长大约与所需“轨道”大小相同的父元素,并给它一个50%的border-radius创建一个圆。这使得元素上的任何一点与其他点距离相等。我们将其设置为position: relative元素,以便我们可以将position: absolute应用于其任何子元素。

通过将我们的子元素固定在父元素上的特定点上,我们创建了所需的距离,从父元素的transform-origin中心的X/Y开始,并应用rotate transform来旋转父元素。但是,如果我们仅这样做,那么子元素也会跟随该路径而不保持竖直(假设所需的是竖直方向),因此我们只需将同一动画应用于父元素,但反转其旋转即可抵消其旋转。结果是一个父元素朝一个方向旋转,而子元素朝另一个方向旋转,从而创建了示例中看到的效果。希望这有所帮助!

#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>


5

我曾经做过的事情,可能与您正在寻找的内容接近:

// 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接