带有淡入淡出效果的 CSS 图像滑块轮播 - 如何获得正确的计时?

问题描述 投票:0回答:1

我正在尝试使用 CSS 创建一个图像轮播,其中每个图像出现四秒钟,然后交叉淡入淡出到下一个图像。我想包含三个循环图像。

我在另一篇文章中找到了似乎接近我需要的代码,但它适用于五张图像。我认为将其编辑为只有三个功能会很容易,但我对 CSS 中的动画时序不够熟悉,所以把它弄得一团糟。现在它显示了三个图像,但每个图像的显示时间不够长,并且淡出/淡入动画不重叠,在每个图像之间留下了间隙。

.crossfade>figure {
  animation: imageAnimation 12s linear infinite 0s;
  backface-visibility: hidden;
  background-size: cover;
  background-position: center center;
  color: transparent;
  height: 100%;
  left: 0px;
  opacity: 0;
  position: absolute;
  top: 0px;
  width: 100%;
  z-index: 0;
}

.crossfade {
  height: 450px;
  width: 100%;
}

.crossfade>figure:nth-child(1) {
  background-image: url('img1.jpg');
}

.crossfade>figure:nth-child(2) {
  animation-delay: 4s;
  background-image: url('img2.jpg');
}

.crossfade>figure:nth-child(3) {
  animation-delay: 8s;
  background-image: url('img3.jpg');
}

@keyframes imageAnimation {
  0% {
    animation-timing-function: ease-in;
    opacity: 0;
  }
  8% {
    animation-timing-function: ease-out;
    opacity: 1;
  }
  17% {
    opacity: 1
  }
  25% {
    opacity: 0
  }
  100% {
    opacity: 0
  }
}
<div class="crossfade">
  <figure></figure>
  <figure></figure>
  <figure></figure>
</div>

html css slider css-animations carousel
1个回答
0
投票

您需要更新百分比。像这样的东西:

@keyframes imageAnimation {
  0% {
    animation-timing-function: ease-in;
    opacity: 0;
  }
  10% { 
    animation-timing-function: ease-out;
    opacity: 1;
  }
  33% {
    opacity: 1;
  }
  66% {
    opacity: 0;
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.