围绕一个中心CSS动画环绕的两个div

问题描述 投票:1回答:2

[我正在尝试使两个单独的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>

Jsfiddle

html css css-animations keyframe
2个回答
3
投票

让您的父元素作为指导;

#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: rotations 6s linear infinite 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>

4
投票

我很多年前所做的事情可能与您正在寻找的东西接近:

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

© www.soinside.com 2019 - 2024. All rights reserved.