为CSS fling-minislide添加标题

时间:2018-05-02 17:37:19

标签: html css

我在网上发现了这个simple CSS slider。有没有办法为每张图片添加不同的标题?



.fling-minislide {width:300px; height:300px; overflow:hidden; position:relative; }
.fling-minislide img{ position:absolute; animation:fling-minislide 20s infinite; opacity:0; width: 100%; height: auto;}

@keyframes fling-minislide {25%{opacity:1;} 40%{opacity:0;}} 
.fling-minislide img:nth-child(4){animation-delay:0s;}
.fling-minislide img:nth-child(3){animation-delay:5s;}
.fling-minislide img:nth-child(2){animation-delay:10s;}
.fling-minislide img:nth-child(1){animation-delay:15s;}

<div class="fling-minislide">
  <img src="https://images.icanvas.com/list-square/abstract-photography-1.jpg" alt="Slide 4" />
  <img src="https://images.icanvas.com/list-square/abstract-photography-2.jpg" />

  <img src="https://images.icanvas.com/list-square/abstract-landscapes.jpg" alt="Slide 2" />

  <img src="https://images.icanvas.com/list-square/abstract-expressive.jpg" alt="Slide 1" />
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

由于动画已应用于imgimg标记不支持结束标记,请将整个包装在div中并在img可以p /之后添加文本span等...然后将动画应用于div。

&#13;
&#13;
.fling-minislide {
    width: 300px;
    height: 300px;
    overflow: hidden;
    position: relative;
}

.fling-minislide div {
    animation: fling-minislide 20s infinite;
    opacity: 0;
    position: absolute;
     width: 300px;
    height: 300px;
}
.fling-minislide div p{
    position: absolute;
    bottom:0;
    left:25%;
    font-weight:900;
    font-size:3rem;
}
.fling-minislide img {
    position: absolute;
    width: 100%;
     height: auto;
}


@keyframes fling-minislide {
    25% {
        opacity: 1;
    }

    40% {
        opacity: 0;
    }
}

.fling-minislide div:nth-child(4) {
    animation-delay: 0s;
}

.fling-minislide div:nth-child(3) {
    animation-delay: 5s;
}

.fling-minislide div:nth-child(2) {
    animation-delay: 10s;
}

.fling-minislide div:nth-child(1) {
    animation-delay: 15s;
}
&#13;
<div class="fling-minislide">
	<div>
	  	<img src="https://images.icanvas.com/list-square/abstract-photography-1.jpg" alt="Slide 4" />
	  	<p>text1</p>
	</div>
	<div>
	  <img src="https://images.icanvas.com/list-square/abstract-photography-2.jpg" /><p>text2</p>
	</div>
	<div>
	  <img src="https://images.icanvas.com/list-square/abstract-landscapes.jpg" alt="Slide 2" /><p>text3</p>
	</div>
	<div>
	  <img src="https://images.icanvas.com/list-square/abstract-expressive.jpg" alt="Slide 1" /><p>text4</p>
	</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

一种解决方案是将图像包装在<figure>中,然后使用<figcaption>来包装文本。

通过Google搜索“图片滑块标题”找到此示例:https://codepen.io/dudleystorey/pen/kFoGw

只需确保通过继承将CSS更改为正确定位。

我做了this working sample on CodePen

要记住的一些事项:  *修改主容器的高度,在这种情况下为.fling-minislide,以便为标题留出空间。否则,如果您希望它显示在图像上,则必须修改包装器,图形和标题的z-index和位置。  *修改您的css继承以定位图片周围的包装,在您的情况下.fling-minislide figure而不是.fling-minislide img  *修改动画的继承以定位包装器,在本例中为.fling-minislide figure:nth-child(4)  *在标题中添加所需的位置和高度,在我的例子中我做了这个

figcaption {
    height: 100px;
    position: absolute;
    top: 300px; /* height of the image */
    left:0; 
}

请注意,位置是根据图像的高度决定的。

我还限制了图像的高度,以便它们都可以显示在相同的位置。

figure img {
    height: 300px;
    overflow: hidden;
    Width: auto;
}