将图像排列为"内联块",底部有悬停标题

Joh*_*n M 5 html css image hover

我正在开发一个图像库,并希望以下列方式在图像底部显示标题:

  • 默认情况下,图像显示时没有任何文本

  • 将鼠标悬停在图像上时,底部会出现一个(可能是截断的)标题,位于深灰色的半透明背景上

  • 最好,我的HTML保持不变; 最重要的是,图像保持为'display:inline-block',因为布局需要它们.

  • (可选)当鼠标悬停在标题上时(如果它被截断),它将完全展开

  • (可选)标题可以包含链接/整个图像是一个链接

请看图解说明:
图形说明

这有点类似于http://www.flickr.com/explore和许多其他网站的做法.

这是我到目前为止所做的(实际上并不太多,因为它在垂直中间呈现标题,而不是在底部):

.image-block {
    display: inline-block;
    margin: 0 0 0 0;
}

.container { /* testing only */
    width: 1555px;
    overflow: scroll;
}



.hover-me {
   position: relative;

}

.hover-me img{
    width:100%;
    height:auto;
    display: block;
}

.hover-me:after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
    background: rgba(128,128,128,.5);
    transition: all .5s ease;
    opacity: 0;
}

.hover-me .caption {
    display: block;
    height: 50%;
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    margin-top: -10px;
    text-align: center;
    transition: all .5s ease;
    opacity: 0;
    z-index: 2;
    color: #fff;
}

.hover-me:hover:after , .hover-me:hover .caption {
    opacity: 1;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <span  class="image-block hover-me ng-scope" style="padding-right: 5px; padding-bottom: 5px; height: 257px; width: 269px;">
            <img class="hovertext" width="269" height="257" src="http://i.imgur.com/zOEilgll.jpg">
           <span class="caption">This is a longish text of what looks like a camel; really quote a long long long long long long long long long long long long text</span>
    </span><span  class="image-block hover-me ng-scope" style="padding-right: 5px; padding-bottom: 5px; height: 257px; width: 385px;">
            <img class="hovertext" width="385" height="257" src="http://i.imgur.com/jj1dY0sl.jpg">
           <span class="caption">Well this is quite a pretty picture too</span>
    </span><span  class="image-block hover-me ng-scope" style="padding-right: 10px; padding-bottom: 5px; height: 257px; width: 396px;">
            <img class="hovertext" width="396" height="257" src="http://i.imgur.com/yVlWIc0l.jpg">
           <span class="caption">Omg what is this a black and white picture</span>
    </span><span  class="image-block hover-me ng-scope" style="padding-right: 0px; padding-bottom: 5px; height: 257px; width: 456px;">
            <img class="hovertext" width="456" height="257" src="http://i.imgur.com/roRmFJWl.jpg">
           <span class="caption">This is just an ordinary truck, I think... maybe; but the discription is really quite long</span>
    </span><span  class="image-block hover-me ng-scope" style="padding-right: 5px; padding-bottom: 5px; height: 289px; width: 433px;">
            <img class="hovertext" width="433" height="289" src="http://i.imgur.com/yo2WhKFl.jpg">
           <span class="caption">A great quote frm a great explorer</span>
    </span>
    <span>More images follow...</span>
</div>
Run Code Online (Sandbox Code Playgroud)

Mr.*_*ien 3

嗯,这很简单,你需要在这里使用 CSS 定位,使用position: relative;容器包装元素...我从头开始制作这个,看看它对你是否有用,如果你需要任何修改,只需 ping 我..

说明:首先创建.wrap元素position: relative;只是为了确保嵌套absolute定位span保持相对于容器。

在下一个选择器中,.wrap span即使用带有负边距的position: absolute;for span,这是故意溢出的,以便它隐藏。

来到下一个选择器 ie.wrap:hover span将显示元素的第一行span

最后一个选择器.wrap:hover span:hover将显示其余的文本。

演示

演示 2 [1] 固定white-space在底部

.wrap {
    position: relative;
    display: inline-block;
    overflow: hidden;
}

.wrap span {
    position: absolute;
    bottom: -70px;
    background: rgba(0,0,0,.8);
    color: #fff;
    left: 0;
    width: 100%;
    -moz-transition: all .5s;
    -webkit-transition: all .5s;
    transition: all .5s;
}

.wrap:hover span {
    bottom: -40px;
}

.wrap:hover span:hover {
    bottom: 0;
}
Run Code Online (Sandbox Code Playgroud)
.wrap img {
    display: block;
}
Run Code Online (Sandbox Code Playgroud)

[1] 将其添加到您的代码中以修复white-space图像底部的问题。

注意:尺寸是固定的,如果您认为每个缩略图的文本可能会有很大差异,那么我建议您使用 jQuery 并相应地设置height元素的span