gpt4 book ai didi

html - 如何在球通过时进行环/箍翻转?

转载 作者:技术小花猫 更新时间:2023-10-29 12:19:26 25 4
gpt4 key购买 nike

我正在尝试做这样的事情:

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>

最佳答案

我会使用两个元素(底部和顶部)创建环,以便能够以不同方式调整每个元素的 z-index:

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

如果您需要比 clip-path 更好的支持,这是另一个想法。诀窍是使用透明颜色:

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

关于html - 如何在球通过时进行环/箍翻转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54979433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com