gpt4 book ai didi

html - 使用关键帧动画的文本 slider

转载 作者:太空宇宙 更新时间:2023-11-03 22:13:28 24 4
gpt4 key购买 nike

我有一个 slider ,其中包含 3 个元素,其中一切正常,如我所愿

这里是现场演示 working demo

包含 3 个元素的文本 slider

HTML

    <span class="item-1">FAST.</span>
<span class="item-2">SIMPLE.</span>
<span class="item-3">PERSONAL.</span>

CSS

body {
font-family: 'Open Sans', 'sans-serif';
color: #cecece;
background: #222;
overflow: hidden;
}

.item-1,
.item-2,
.item-3 {
position: absolute;
display: block;
top: 2em;
width: 60%;
font-size: 2em;
animation-duration: 20s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}

.item-1 {
animation-name: anim-1;
}

.item-2 {
animation-name: anim-2;
}

.item-3 {
animation-name: anim-3;
}

@keyframes anim-1 {
0%,
8.3% {
top: -100%;
opacity: 0;
}
8.3%,
25% {
top: 25%;
opacity: 1;
}
33.33%,
100% {
top: 110%;
opacity: 0;
}
}

@keyframes anim-2 {
0%,
33.33% {
top: -100%;
opacity: 0;
}
41.63%,
58.29% {
top: 25%;
opacity: 1;
}
66.66%,
100% {
top: 110%;
opacity: 0;
}
}

@keyframes anim-3 {
0%,
66.66% {
top: -100%;
opacity: 0;
}
74.96%,
91.62% {
top: 25%;
opacity: 1;
}
100% {
top: 110%;
opacity: 0;
}
}

现在我想在 slider 上添加两个元素

HTML

        <span class="item-1">FAST.</span>
<span class="item-2">SIMPLE.</span>
<span class="item-3">PERSONAL.</span>
<span class="item-4">SOCIAL.</span>
<span class="item-5">LOUD.</span>

CSS

body {
font-family: 'Open Sans', 'sans-serif';
color: #cecece;
background: #222;
overflow: hidden;
}

.item-1,
.item-2,
.item-3,
.item-4,
.item-5{
position: absolute;
display: block;
top: 2em;
width: 60%;
font-size: 2em;
animation-duration: 20s;
animation-timing-function: ease-in-out;
animation-iteration-count: infinite;
}

.item-1 {
animation-name: anim-1;
}

.item-2 {
animation-name: anim-2;
}

.item-3 {
animation-name: anim-3;
}
.item-4{
animation-name: anim-4;
}
.item-5{
animation-name: anim-5;
}

@keyframes anim-1 {
0%,
6.3% {
top: -100%;
opacity: 0;
}
6.3%,
25% {
top: 25%;
opacity: 1;
}
13.33%,
100% {
top: 110%;
opacity: 0;
}
}

@keyframes anim-2 {
0%,
23.33% {
top: -100%;
opacity: 0;
}

31.63%,
48.29% {
top: 25%;
opacity: 1;
}

56.66%,
100% {
top: 110%;
opacity: 0;
}

}

@keyframes anim-3 {
0%,
56.66% {
top: -100%;
opacity: 0;
}
64.96%,
71.62% {
top: 25%;
opacity: 1;
}
100% {
top: 110%;
opacity: 0;
}
}

@keyframes anim-4 {
0%,
71.66% {
top: -100%;
opacity: 0;
}
84.96%,
91.62% {
top: 25%;
opacity: 1;
}
100% {
top: 110%;
opacity: 0;
}
}

@keyframes anim-5 {
0%,
84.96% {
top: -100%;
opacity: 0;
}

94.96%,
91.62% {
top: 25%;
opacity: 1;
}

100% {
top: 110%;
opacity: 0;
}
}

这里有五个元素的演示

not working demo

我的代码需要更改什么?

最佳答案

问题是由于五种不同动画的百分比困惑所致。

为什么不重复使用相同的动画,比如:

@keyframes anim {
0%, 33.33% {
top: -100%;
opacity: 0;
}
33.33%, 50% {
top: 25%;
opacity: 1;
}
50%, 100% {
top: 110%;
opacity: 0;
}
}

然后在每个 span 上应用 animation-delay,例如:

.item-1 { animation-delay: 0s }
.item-2 { animation-delay: 4s }
.item-3 { animation-delay: 8s }
.item-4{ animation-delay: 12s }
.item-5{ animation-delay: 16s }

Here是一个工作示例。

提示 请记住,动画化 top 值并不是性能方面的最佳选择。尽可能尝试为 transformopacity 值设置动画。

关于html - 使用关键帧动画的文本 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58058854/

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