Как сделать так, чтобы кольцо/обруч переворачивалось при прохождении через него шара? - программирование

Как сделать так, чтобы кольцо/обруч переворачивалось при прохождении через него шара?

Я пытаюсь сделать что-то вроде этого:

animation of a ball passing through a hoop

но мяч, кажется, не проходит через кольцо, а проходит через кольцо. Как я могу исправить эту проблему?

body {
  height: 50em;
}

.ring {
  position: relative;
  width: 200px;
  height: 100px;
  border-radius: 50%;
  border: 10px solid #ffcf82;
  z-index: 9
}

@keyframes spinner {
  0% {
    transform: rotateZ(0deg);
  }
  30% {
    transform: rotateZ(0deg);
  }
  60% {
    transform: rotateZ(180deg);
  }
}

@keyframes translate {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-370px);
  }
}

.ring {
  animation-name: spinner;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  transform-style: preserve-3d;
}

.ball {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #14e78e;
  margin: 100px;
}

.ball {
  animation-name: translate;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  animation-duration: 8s;
  transform-style: preserve-3d;
}
<div class="ring"></div>
<div class="ball"></div>
4b9b3361

Ответ 1

Вы можете попробовать изменить z-индекс шара внутри анимации.

body {
  height: 50em;
}

.ring {
  position: relative;
  width: 200px;
  height: 100px;
  border-radius: 50%;
  border: 10px solid #ffcf82;
  z-index: 9
}

@keyframes spinner {
  0% {
    transform: rotateZ(0deg);
  }
  30% {
    transform: rotateZ(0deg);
  }
  60% {
    transform: rotateZ(180deg);
  }
}

@keyframes translate {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-370px);
    z-index: 10;
  }
}

.ring {
  animation-name: spinner;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  transform-style: preserve-3d;
}

.ball {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #14e78e;
  margin: 100px;
  position: relative;
}

.ball {
  animation-name: translate;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  animation-duration: 8s;
  transform-style: preserve-3d;
}
<div class="ring"></div>
<div class="ball"></div>

Ответ 2

Я бы создал кольцо, используя два элемента (нижнюю и верхнюю часть), чтобы можно было по-разному регулировать z-индекс каждого из них:

.ring {
  margin-top:80px;
  position: relative;
  width: 200px;
  height: 100px;
}
.ring:before,
.ring:after{
  content:"";
  position:absolute;
  left:0;
  right:0;
  height:100%;
  border: 10px solid #ffcf82;
  border-radius:50%;
  box-sizing:border-box;
}
.ring:before {
   z-index:-1;
   clip-path: polygon(0 0, 100% 0, 100% 50%, 0 50%);
}
.ring:after {
   z-index:1;
   clip-path: polygon(0 100%, 100% 100%, 100% 50%, 0 50%);
}

@keyframes spinner {
  0%,50% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(-180deg);
  }
}

@keyframes translate {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-300px);
  }
}

.ring:before,
.ring:after{
  animation: spinner infinite alternate 4s;
}

.ball {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #14e78e;
  margin: 60px 80px;
  position:relative;
  z-index:0;
  animation: translate 8s infinite linear;
}
<div class="ring"></div>
<div class="ball"></div>

Ответ 3

Вы можете использовать 3d-трансформацию, чтобы автоматически получить этот эффект.

Поверните круг по оси X. Тогда есть одна часть этого, которая находится позади самолета, и другая часть, которая находится перед ним. Мяч все еще находится в плоскости 0 z, поэтому он, естественно, пересекает круг:

body {
  height: 50em;
  transform-style: preserve-3d;
}

.ring {
  position: relative;
  width: 200px;
  height: 100px;
  border-radius: 50%;
  border: 10px solid #ffcf82;
  z-index: 9;
  margin-top: 100px;
      transform: rotateX(50deg) rotateY(0deg) ;
  transform-style: preserve-3d;
}

@keyframes spinner {
  0%, 30% {
    transform: rotateX(50deg) rotateY(0deg);
  }
  60%, 100% {
    transform: rotateX(50deg) rotateY(180deg);
  }
}

@keyframes translate {
  0% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-370px);
  }
}

.ring {
  animation-name: spinner;
  animation-timing-function: ease-in-out;
  animation-iteration-count: infinite;
  animation-duration: 5s;
  transform-style: preserve-3d;
}

.ball {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #14e78e;
  margin: 100px;
}

.ball {
  animation-name: translate;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
  animation-duration: 8s;
  transform-style: preserve-3d;
}
<div class="ring"></div>
<div class="ball"></div>