在幻灯片图像旁边添加文本

时间:2018-02-15 14:49:10

标签: html css css3 flexbox css-transitions

这是一个代码我修改了一下,只使用HTML和CSS制作了一个简单的幻灯片图片:

(这是代码的一部分,请点击演示)

代码:



.slideshow {
  width: 800px;
  margin: 0 auto;
  overflow: hidden;
  border: solid 1px white;
}

.slideshow-container {
  width: 2500px;
  font-size: 0;
  transition: 1s ease;
  height: 225px;
}

@keyframes slide {
  0% {
    transform: translateX(0%);
  }
  12.5% {
    transform: translateX(0%);
  }
  25% {
    transform: translateX(-25%);
  }
  37.5% {
    transform: translateX(-25%);
  }
  50% {
    transform: translateX(-50%);
  }
  62.5% {
    transform: translateX(-50%);
  }
  75% {
    transform: translateX(-75%);
  }
  87.5% {
    transform: translateX(-75%);
  }
  99% {
    transform: translateX(-75%);
  }
  100% {
    transform: translateX(0);
  }
}

<section class="slideshow">
  <div class="slideshow-container slide">
    <img src="http://placeimg.com/625/225/any" />
    <img src="http://placeimg.com/625/225/animals" />
    <img src="http://placeimg.com/625/225/arch" />
  </div>
</section>
&#13;
&#13;
&#13;

demo

我想在每个图像旁边添加一个文本,所以在框架中我将有一半的图像和另一半文本。 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

您将imagetext部分包装到.item div中。

display:flex应用于父.slideshow-container,以便连续设置.item

然后只需将width:50%应用到img.caption div,将其对齐屏幕的一半

还根据连续3个项目更改了动画动画分区值

<强> Fiddle Link

Stack Snippet

&#13;
&#13;
/*general styles*/

body {
  padding: 3em;
  background-color: #ccc;
}

html {
  box-sizing: border-box;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}


/*slideshow styles*/

.slideshow {
  width: 800px;
  margin: 0 auto;
  overflow: hidden;
  border: solid 1px white;
}

.item {
  display: flex;
  width: 800px;
  align-items: center;
}

.slideshow-container {
  width: 2400px;
  transition: 1s ease;
  display: flex;
}

.slideshow-container:hover {
  animation-play-state: paused;
}

.slide {
  animation: slide 24s ease infinite;
}

@keyframes slide {
  0% {
    transform: translateX(0%);
  }
  25% {
    transform: translateX(0);
  }
  37.5% {
    transform: translateX(-33.333%);
  }
  62.5% {
    transform: translateX(-33.333%);
  }
  75% {
    transform: translateX(-66.667%);
  }
  99% {
    transform: translateX(-66.667%);
  }
  100% {
    transform: translateX(0);
  }
}

.item img {
  width: 50%
}

.item .caption {
  width: 50%
}
&#13;
<!--hovering over the images will pause the slideshow -->

<section class="slideshow">
  <div class="slideshow-container slide">
    <div class="item">
      <img src="http://placeimg.com/625/225/any" />
      <div class="caption">Text</div>
    </div>
    <div class="item">
      <img src="http://placeimg.com/625/225/animals" />
      <div class="caption">Text</div>
    </div>

    <div class="item">
      <img src="http://placeimg.com/625/225/arch" />
      <div class="caption">Text</div>
    </div>
  </div>
</section>
&#13;
&#13;
&#13;