首页 > 解决方案 > CSS淡入/淡出背景图像显示空白最后一个图像

问题描述

我一直在尝试将仅更改背景图像的 CSS 应用到 div,但由于某种原因,在指定的三个图像结束时,我得到一个背景只是原始黑色背景颜色的时期。更奇怪的是,如果我将转换时间切换为 4 秒,其中一张图像根本不会显示。有没有人有办法解决吗?该页面可以在这里找到(链接已删除)

CSS:* {填充:0;边距:0 }

body {
  background-color: #000000;
}

.crossfade > figure {
  animation: imageAnimation 30s 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 > figure:nth-child(1) {
  background-image: url('assets/img/landing/gym_1.jpg');
}
.crossfade > figure:nth-child(2) {
  animation-delay: 6s;
  background-image: url('assets/img/landing/gym_2.jpg');
}
.crossfade > figure:nth-child(3) {
  animation-delay: 12s;
  background-image: url('assets/img/landing/weights.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
  }
}

HTML:

<div class="crossfade">
<figure></figure>
<figure></figure>
<figure></figure>

标签: htmlcss

解决方案


我假设这是你的时间。

动画持续 30 秒,其中 75% 的时间opacity: 0没有过渡。这意味着在 19.5 秒(25% + 12 秒延迟figure:nth-child(3))之后,接下来的 10.5 秒内没有任何可见的东西,直到动画再次开始figure:nth-child(1)

将其更改为所有值都是三分之一应该可以整理(即 66% 的时间已经opacity: 0和更新延迟为10s或持续时间为18s)。或者,您可以有 4figures并将延迟更改为7.5.


推荐阅读