是否有可能不断为DIV旋转木马制作动画?

时间:2018-02-15 02:59:42

标签: html css html5 css3

enter image description here

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

我在网上看过有关如何使用图片进行此操作的示例,但没有看到带有设置宽度的文本的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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:6)

这是一种方法。

.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>