【问题标题】:Adding text next to slideshow images在幻灯片图像旁边添加文本
【发布时间】:2018-02-15 23:59:37
【问题描述】:

这是一个代码,我对其进行了一些修改,以仅使用 HTML 和 CSS 制作带有图像的简单幻灯片:

(这是部分代码查看全部请点击demo)

代码:

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

demo

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

【问题讨论】:

  • hm,尝试制作容器div,包括图像和文本,设置样式。例如 20% 的文本宽度,另外 80% 的图像宽度。
  • 如果我将每个 img 放在一个 div 中,则只有第一张图片不会显示其他图片

标签: html css flexbox css-transitions


【解决方案1】:

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

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

然后只需将width:50% 应用到您的img.caption div 以将它们对齐屏幕的一半

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

Fiddle Link

堆栈片段

/*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%
}
<!--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>

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 2022-09-26
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2011-12-18
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多