Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a ring/hoop flip as a ball passes through it?

I am trying to make something like this:

animation of a ball passing through a hoop

but the ball doesn't seem to pass through the ring, but rather passes across the ring. How can I fix this issue?

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>
like image 424
zubaida farha Avatar asked Mar 04 '19 08:03

zubaida farha


1 Answers

I would create the ring using two elements (the bottom and the top part) to be able to adjust the z-index of each one differently:

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

Another idea in case you need better support than clip-path. The trick is to play with transparent color:

.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;
}
.ring:after {
   z-index:1;
   border-bottom-color:transparent;
   border-right-color:transparent;
}

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

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

.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>
like image 58
Temani Afif Avatar answered Sep 21 '22 16:09

Temani Afif