为什么这个 CSS @keyframes 规则没有淡入淡出?

Why is this CSS @keyframes rule not cross-fading?

我正在尝试创建一个非常简单的幻灯片,在每个图像之间交叉淡入淡出。问题是图像正在淡出 下一张图像开始淡入之前。

我希望每个图像淡入持续 1 秒,再保持 10 秒,然后淡出持续 1 秒 - 总共 12 秒。

@keyframes 是通过将动画持续时间除以 100 计算得出的,即每秒 1.81%。

动画延迟为 11 秒,这让我感到困惑,因为下一张图片应该在最后一张图片淡出时淡入。

如果有人能指出我做错了什么,我将不胜感激。

HTML:

<!doctype html>
<html>
    <body>
        <main>
            <div id="cf">
                <img src="https://placeimg.com/640/480/animals">
                <img src="https://placeimg.com/640/480/nature">
                <img src="https://placeimg.com/640/480/tech">
                <img src="https://placeimg.com/640/480/people">
                <img src="https://placeimg.com/640/480/sepia">
            </div>
        </main>
    </body>
</html>

CSS:

@keyframes crossFade{
    0%{opacity: 0;}
    1.81%{opacity: 1;}
    18.18%{opacity: 1;}
    19.98%{opacity: 0;}
    100%{opacity: 0;}
}

#cf img{
    position:absolute;
    left:0;
    opacity: 0;
    animation-name: crossFade;
    animation-duration: 55s;
    animation-iteration-count: infinite;
}

#cf img:nth-last-of-type(1){
    animation-delay: 0s;
}

#cf img:nth-last-of-type(2){
    animation-delay: 11s;
}

#cf img:nth-last-of-type(3){
    animation-delay: 22s;
}

#cf img:nth-last-of-type(4){
    animation-delay: 33s;
}

#cf img:nth-last-of-type(5){
    animation-delay: 44s;
}

Link 到 Codepen: https://codepen.io/Musicky/pen/YgyOPm

我更改了您的百分比和持续时间。这将像您期望的那样显示。

您可能想调整第一个过渡开始时的长度,但您的原始过渡没有重叠。当事物可分割时,计时更简单。不是说做不到,而是做起来更难

@keyframes crossFade{
    0%{opacity: 0;}
    10%{opacity: 1;}
    20%{opacity: 1;}
    30%{opacity: 0;}
    100%{opacity: 0;}
}
#cf img{
    position:absolute;
    left:0;
    opacity: 0;
    animation-name: crossFade;
    animation-duration: 60s;
    animation-iteration-count: infinite;
}
#cf img:nth-last-of-type(1){
    animation-delay: 0s;
}
#cf img:nth-last-of-type(2){
    animation-delay: 10s;
}
#cf img:nth-last-of-type(3){
    animation-delay: 20s;
}
#cf img:nth-last-of-type(4){
    animation-delay: 30s;
}
#cf img:nth-last-of-type(5){
    animation-delay: 40s;
}
<div id="cf">
    <img src="https://placeimg.com/640/480/animals">
    <img src="https://placeimg.com/640/480/nature">
    <img src="https://placeimg.com/640/480/tech">
    <img src="https://placeimg.com/640/480/people">
    <img src="https://placeimg.com/640/480/sepia">
</div>