【问题标题】:Is it possible to continuously animate a carousel of DIVs?是否可以连续为 DIV 的轮播设置动画?
【发布时间】:2018-07-17 14:58:23
【问题描述】:

假设红色框代表我的网页容器,而 B、C、D 项在容器之外。是否可以仅使用 CSS 让项目(A、B、C、D...)像轮播一样从左到右自动滚动?

我在网上看到了一些例子,说明如何使用图像而不是 DIV 中的充满设置宽度的文本?

.container {
  margin: 0 auto;
  width: 800px;
  background: red;
  padding: 36px;
  box-sizing: border-box;
}

.items {
  display: flex;
}

.item {
    box-sizing: border-box;
    margin-left: 72px;
    margin-right: 72px;
    padding: 36px;  
position: relative;
    width: 200px;
    min-width: 200px;
    background: #efefef;
    text-align: center;
    border-radius: 4px;

}
<div class="container">
  <div class="items">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div> 
    <div class="item">D</div>    
  </div>
 </div>

【问题讨论】:

标签: html css


【解决方案1】:

这是一种方法。

.container {
  margin: 0 auto;
  background: red;
  box-sizing: border-box;
  overflow-x: hidden;
}

.items {
  min-height: 200px;
  position: relative;
}

.item {
  box-sizing: border-box;
  padding: 36px;
  position: absolute;
  width: 30%;
  background: #efefef;
  text-align: center;
  border-radius: 4px;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  animation: slide-item 4s infinite;
  animation-timing-function: cubic-bezier(.4, 0, .2, 1);
  opacity: 0;
}

.item:nth-child(2) { animation-delay: 1s; }
.item:nth-child(3) { animation-delay: 2s; }
.item:nth-child(4) { animation-delay: 3s; }

@keyframes slide-item {
    0% { left: 150%; opacity: 1; }
   36% { left:  50%; opacity: 1; }
   72% { left: -50%; opacity: 1; }
  100% { left: -50%; }
}
<div class="container">
  <div class="items">
    <div class="item">A</div>
    <div class="item">B</div>
    <div class="item">C</div>
    <div class="item">D</div>
  </div>
</div>

【讨论】:

  • 谢谢,但有没有办法让它们不来来去去,而是彼此相邻并循环播放
  • @AnApprentice,是的,有。但我对其进行编码只是为了向您展示它可以完成,而不是让您可以在我们进行时改进您的请求。自己玩时间。阅读文档和网络标准。 (研究。从我的观点来看,在询问之前,您应该自己尝试一下。另外,请记住这不是真正的旋转木马。没有播放/暂停,没有设置。它只是对一些过渡进行硬编码,因此它们看起来像是一个轮播。但是轮播意味着 DOM 操作。这意味着:使用 JavaScript。这就是 JS 的用途。
  • @AnApprentice,将animation-timing-function更改为linear,将width更改为60%,以实现“传送带”效果类型。您可能还想使用@media 查询来更改整体动画周期持续时间和基于页面宽度的延迟。
猜你喜欢
  • 2015-10-22
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多