我正在开发一个图片库,并希望通过以下方式在图片底部显示标题:
图片默认显示时没有任何文字
将鼠标悬停在图像上时,(可能被截断的)标题会出现在深灰色半透明背景的底部
我的 HTML 最好保持原样;最重要的是,图像保持为“显示:内联 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;
}
<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>
最佳答案
好吧,这很简单,你需要在这里使用 CSS Positioning,使用 position: relative;
容器包裹元素......我从头开始做这个,看看它是否对你有用,如果你需要任何修改,只需 ping 我..
说明:首先我制作 .wrap
元素 position: relative;
只是为了确保嵌套的 absolute
定位在 span
保持相对于容器。
在下一个选择器中,即 .wrap span
正在使用 position: absolute;
为 span
设置负边距,这是故意溢出的,以便它隐藏起来。
进入下一个选择器,即 .wrap:hover span
将显示 span
元素的第一行。
最后一个选择器 .wrap:hover span:hover
将显示其余文本。
Demo 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;
}
.wrap img {
display: block;
}
[1] 将此添加到您的代码以修复图像底部的 white-space
。
Note: The dimensions are fixed, if you think the text for each thumbnail may vary to a great extent, than I will recommend you to use jQuery and set the
height
of thespan
elements accordingly.
关于html - 使图像布局为 'inline-block',底部有悬停标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21809336/