gpt4 book ai didi

html - 为什么我的自定义动画不能正常工作?

转载 作者:搜寻专家 更新时间:2023-10-31 08:13:29 24 4
gpt4 key购买 nike

我的自定义@keyframes 动画没有按应有的方式工作。

Block 1之间没有差距和 Block 2旋转时,Block 2之间有一个小间隙和 Block 3 , Block 3Block 4 , 和 Block 4Block 1 .

我不明白为什么上面列出的 block 之间有间隙,或者为什么 block 1 和 block 2 之间没有 block 。我应该怎么做才能消除间隙,我希望它看起来像一个没有间隙的盒子。

对代码的任何其他建议也很感激。下面是代码。

.main {
width: 250px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.main>div {
width: 250px;
height: 100px;
position: absolute;
text-align: center;
line-height: 100px;
color: #fff;
font-weight: bold;
font-family: monospace;
font-size: 2em;
transition: 1s;
cursor: pointer;
}

.child1 {
background: red;
transform-origin: bottom;
animation: animate1 8s infinite;
}

.child2 {
background: blue;
transform-origin: top;
animation: animate2 8s infinite;
}

.child3 {
background: yellow;
transform-origin: bottom;
animation: animate3 8s infinite;
}

.child4 {
background: green;
transform-origin: top;
animation: animate4 8s infinite;
}


/*.main:hover .child1{
transform: translateY(-100%) rotateX(90deg);
}

.main:hover .child2{
transform: translateY(0%) rotateX(0deg);
}*/

@keyframes animate1 {
0% {
transform: translateY(0%) rotateX(0deg);
}
25% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
50% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
75% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
100% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
}

@keyframes animate2 {
0% {
transform: translateY(100%) rotateX(90deg);
}
25% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
50% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
75% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
100% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
}

@keyframes animate3 {
0% {
transform: translateY(-100%) rotateX(90deg);
}
25% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
50% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
75% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
100% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
}

@keyframes animate4 {
0% {
transform: translateY(-100%) rotateX(90deg);
}
25% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
50% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
75% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
100% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
}
<div class="main">
<div class="child1">Block 1</div>
<div class="child2">Block 2!</div>
<div class="child3">Block 3</div>
<div class="child4">Block 4</div>
</div>

最佳答案

这是因为您正在为状态之间的 transform-origin 设置动画,您应该通过添加更多状态来避免这种情况,以便快速更改 transform-origin。通过更改 transform-origin,您可以从逻辑上更改元素的旋转方式,并创建不需要的移动,从而创建您看到的间隙。

为什么在区 block 1 和区 block 2 之间看不到它?

仅仅是因为如果你查看两个元素的第一个状态,你会发现 transform-origin 没有变化,所以没有移动会产生与其他状态不同的间隙:

.main {
width: 250px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.main>div {
width: 250px;
height: 100px;
position: absolute;
text-align: center;
line-height: 100px;
color: #fff;
font-weight: bold;
font-family: monospace;
font-size: 2em;
transition: 1s;
cursor: pointer;
}

.child1 {
background: red;
transform-origin: bottom;
animation: animate1 8s infinite;
}

.child2 {
background: blue;
transform-origin: top;
animation: animate2 8s infinite;
}

.child3 {
background: yellow;
transform-origin: bottom;
animation: animate3 8s infinite;
}

.child4 {
background: green;
transform-origin: top;
animation: animate4 8s infinite;
}

@keyframes animate1 {
0% {
transform-origin: bottom;
transform: translateY(0%) rotateX(0deg);
}
25% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
50% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
75% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
100% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
}

@keyframes animate2 {
0% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
25% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
25.5% {
transform-origin: bottom;
transform: translateY(0%) rotateX(0deg);
}
50% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
75% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
75.5% {
transform-origin: top;
transform: translateY(-100%) rotateX(90deg);
}
100% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
}

@keyframes animate3 {
0% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
0.5% {
transform-origin: top;
transform: translateY(-100%) rotateX(90deg);
}
25% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
50% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
50.5% {
transform-origin: bottom;
transform: translateY(0%) rotateX(0deg);
}
75% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
100% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
}

@keyframes animate4 {
0% {
transform-origin: top;
transform: translateY(-100%) rotateX(90deg);
}
0.5% {
transform-origin: top;
transform: translateY(-100%) rotateX(90deg);
}
25% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
25.5% {
transform-origin: top;
transform: translateY(-100%) rotateX(90deg);
}
50% {
transform-origin: top;
transform: translateY(100%) rotateX(90deg);
}
75% {
transform-origin: top;
transform: translateY(0%) rotateX(0deg);
}
75.5% {
transform-origin: bottom;
transform: translateY(0%) rotateX(0deg);
}
100% {
transform-origin: bottom;
transform: translateY(-100%) rotateX(90deg);
}
}
<div class="main">
<div class="child1">Block 1</div>
<div class="child2">Block 2!</div>
<div class="child3">Block 3</div>
<div class="child4">Block 4</div>
</div>

关于html - 为什么我的自定义动画不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50105855/

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