围绕中心 CSS 动画运行的两个 div

Fak*_*ire 2 html css keyframe css-animations

我试图围绕一个中心以圆周运动“环绕”两个单独的 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);
  }
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
  <div class="box">
  </div>
  <div class="box">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

提琴手

Chr*_* W. 6

让您的父元素成为指南;

当目标是围绕中心以一致的间距旋转时(而不是说“椭圆轨道”更像是椭圆形图案),而不是最简单的技术是让父级设置一致的边界并将子级附加在其中使用它的位置作为他们的动画路径。我们的目标只是提供一种单个元素同步轨道的错觉,而实际上只有一个元素在旋转,其默认transform-origin中心作为“绕”在其中的孩子的指南。

在我们的例子中,我们选取​​了一个父级,其周长大致等于“期望轨道”的大小,我们给它border-radius50% 的 a 来创建一个圆。这使得元素上的点与另一个点的距离不小于或大于点。我们使它成为一个position: relative元素,以便我们可以应用于position: absolute它的任何子元素。在这个例子中,我们使用了伪元素,但它们也可以很容易地成为额外的 DOM 节点元素,比如 div。

通过将我们的孩子固定到父级上的特定点,我们创建了transform-origin与我们希望的父级中心的 X/Y 相等的距离,并应用rotate transform来旋转父级。但是,如果我们只这样做,那么孩子们也将遵循该路径,而不是保持垂直(因为它假设是需要的),所以我们只需重新使用应用于父级的相同动画,但reverse以抵消其旋转。结果是父元素向一个方向旋转,子元素向另一个方向旋转以创建示例中看到的效果。希望这可以帮助!

#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) }
}
Run Code Online (Sandbox Code Playgroud)
<div id="rotator"></div>
Run Code Online (Sandbox Code Playgroud)


Sea*_*nik 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;
  }
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

https://codepen.io/seanstopnik/pen/93f9cbcbcf9b38684bfc75f38c9c4db3