【问题标题】:CSS slideshow issues with last image not showing up but for a split second最后一张图片的CSS幻灯片问题没有显示,但一瞬间
【发布时间】:2016-01-05 13:48:03
【问题描述】:

我创建了一个幻灯片,其中包含来自其他非常有才华的人的大量输入(我正在学习)。但是,我的代码似乎一直到第五张图片,但很快又回到了第一张,而不是与前四张幻灯片保持相同的时间表。我试图避免使用 JS 或 jQuery。我觉得它更容易阅读,尤其是考虑到我的 Java 背景很少。

这是我目前使用的代码:

@import url(http://fonts.googleapis.com/css?family=Istok+Web);
@keyframes slidy {
  0% {
    left: 0%;
  }
  20% {
    left: 0%;
  }
  25% {
    left: -100%;
  }
  45% {
    left: -100%;
  }
  50% {
    left: -200%;
  }
  70% {
    left: -200%;
  }
  75% {
    left: -300%;
  }
  95% {
    left: -300%;
  }
  100% {
    left: -400%;
  }
}
body,
figure {
  margin: 0;
  background: #101010;
  font-family: Istok Web, sans-serif;
  font-weight: 100;
}
div#captioned-gallery {
  width: 100%;
  overflow: hidden;
}
figure.slider {
  position: relative;
  width: 500%;
  font-size: 0;
  animation: 60s slidy infinite;
}
figure.slider figure {
  width: 20%;
  height: auto;
  display: inline-block;
  position: inherit;
}
figure.slider img {
  width: 100%;
  height: 95%;
}
figure.slider figure figcaption {
  position: absolute;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
  width: 100%;
  font-size: 2rem;
  padding: .6rem;
}
<div id="captioned-gallery">
  <figure class="slider">
    <figure>
      <img src="http://lorempixel.com/800/600/nature/1">
    </figure>
    <figure>
      <img src="http://lorempixel.com/800/600/nature/2">
    </figure>
    <figure>
      <img src="http://lorempixel.com/800/600/nature/3">
    </figure>
    <figure>
      <img src="http://lorempixel.com/800/600/nature/4">
    </figure>
    <figure>
      <img src="http://lorempixel.com/800/600/nature/5">
    </figure>
    <figure>
      <img src="http://lorempixel.com/800/600/nature/6">
    </figure>
  </figure>
</div>

所以我认为关键帧部分一定有问题。但是,我认为我可能只是在这方面很糟糕,并且很难找到对我有意义的好参考。我弄乱了百分比,但似乎无法正确处理。我主要需要知道关键帧是如何工作的,以及工作是如何在每张幻灯片中分配的。我什至想知道如何在组合中添加另一张幻灯片,但解决第一个问题将是一个很好的开始。我很感激你花时间来剖析这个。非常感谢所有帮助。

【问题讨论】:

    标签: html css slideshow css-animations


    【解决方案1】:

    在我解释为什么最后一张图片只显示一瞬间的原因之前,需要进行一些其他更改才能使幻灯片正常工作:

    • 所发布的 CSS 似乎是在假设只有 5 个图像但 HTML 标记有六个 .slider figure 元素的情况下进行编码的。我不确定这是故意还是错误,但我将根据提供的 HTML 解释更改。
    • 首先,将figure.slider的宽度从500%改为600%,因为有六张图片,每张图片的宽度都必须是100%宽度(即覆盖整个身体)。这应始终计算为(元素数量 * 100%)。如果不这样做,一些图像将换行到下一行,并且由于父级上的overflow: hidden 设置而被隐藏。
    • 接下来,figure.slider figure 的宽度应该从 20% 更改为 16.67%,因为我们现在有六个图像,所以每个图像应该有 100% 的 1/6th 作为其宽度。
    • 当我们有 5 张图片时,left: 0% 将显示第一张图片,left: -100% 将显示第二张nd 图像,因此仅在left: -400% 显示第五张图片将显示。根据所提供的代码,left: -300% 到 left: -400% 的移动仅发生在 95% 到 100% 之间,因此第 5 张图像仅在 100% 处完全可见,这与结束的时间相同动画和这就是为什么最后一张图片只显示一瞬间。
    • 为了解决上述问题,应更改keyframe 规则。因为对于我们的案例,我们拍摄了 6 张图像,我们需要 5 (n-1) 次滑动移动,并且每次滑动移动都在动画持续时间的5% 上完成。这意味着总共有25% 的持续时间被滑动运动用完。现在剩余的75% 应该在图像元素之间平均分配,以便它们中的每一个在消失前都可以看到 x 秒。因此,我们得到12.5% 每张图像的停留时间和5% 幻灯片移动时间。对@keyframes 进行修改后,我们可以看到每张图片都获得了它们的屏幕时间(即 60 秒的 12.5% = 7.5 秒)。

    @import url(http://fonts.googleapis.com/css?family=Istok+Web);
    @keyframes slidy {
      0% {
        left: 0%;
      }
      12.5% {
        left: 0%;
      }
      17.5% {
        left: -100%;
      }
      30% {
        left: -100%;
      }
      35% {
        left: -200%;
      }
      47.5% {
        left: -200%;
      }
      52.5% {
        left: -300%;
      }
      65% {
        left: -300%;
      }
      70% {
        left: -400%;
      }
      82.5% {
        left: -400%;
      }  
      87.5% {
        left: -500%;
      }
      100% {
        left: -500%;
      }  
    }
    body,
    figure {
      margin: 0;
      background: #101010;
      font-family: Istok Web, sans-serif;
      font-weight: 100;
    }
    div#captioned-gallery {
      width: 100%;
      overflow: hidden;
    }
    figure.slider {
      position: relative;
      width: 600%;
      font-size: 0;
      animation: 60s slidy infinite;
    }
    figure.slider figure {
      width: 16.66%;
      height: auto;
      display: inline-block;
      position: inherit;
    }
    figure.slider img {
      width: 100%;
      height: 95%;
    }
    figure.slider figure figcaption {
      position: absolute;
      bottom: 0;
      background: rgba(0, 0, 0, 0.4);
      color: #fff;
      width: 100%;
      font-size: 2rem;
      padding: .6rem;
    }
    <div id="captioned-gallery">
      <figure class="slider">
        <figure>
          <img src="http://lorempixel.com/800/600/nature/1">
        </figure>
        <figure>
          <img src="http://lorempixel.com/800/600/nature/2">
        </figure>
        <figure>
          <img src="http://lorempixel.com/800/600/nature/3">
        </figure>
        <figure>
          <img src="http://lorempixel.com/800/600/nature/4">
        </figure>
        <figure>
          <img src="http://lorempixel.com/800/600/nature/5">
        </figure>
        <figure>
          <img src="http://lorempixel.com/800/600/nature/6">
        </figure>
      </figure>
    </div>

    【讨论】:

    • 哈利!老兄!你摇滚!迄今为止最好的解释!我一直在试图弄清楚这些东西,你让它听起来简单多了。太感谢了。我尝试了代码,它就像一个魅力。现在,如果我需要在@keyframes 中编辑任何内容,我知道该怎么做。我非常感谢您的帮助。
    • 很好的答案,您的第 5 点清楚地说明了关键帧移动的计算。 +1
    猜你喜欢
    • 2013-08-24
    • 2017-11-02
    • 2020-08-15
    • 1970-01-01
    • 2021-01-17
    • 2013-04-29
    • 2018-11-09
    • 1970-01-01
    • 2022-08-22
    相关资源
    最近更新 更多