html - 如何在悬停多个缩略图图像时设置黑色透明度,每个图像都有不同的位置?

标签 html css image hover transparency

我正在尝试在悬停多个缩略图图像时设置多个黑色透明图像,并且每个图像都有不同的位置。为了确保这一点,每个图像都有自己的类(img1、img2、img3...),并定义了不同的边距。

HTML

<div data-content="Text here" class="image">
 <div class="img1">
  <a href="site1.html"><img src="../img1"></a>
 </div>
</div>
<div data-content="Text here" class="image">
 <div class="img2">
  <a href="site2.html"><img src="../img2"></a>
 </div>
</div>

CSS IMG

.img1 {
  height:200px;
  position:relative;
  margin-top:-10%;

}
.img2 {
  height:200px;
  position:relative;
  left:10%;
}

CSS 透明度

.image {
    position:relative;
    width:200px;
    height:200px;
}
.image img {
    width:100%;
    vertical-align:top;
}
.image:after {
    content: attr(data-content);
    color:#fff;
    position:absolute;
    width:100%; height:100%;
    top:0; left:0;
    background:rgba(0,0,0,0.6);
    opacity:0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}
.image:hover:after {
    opacity:1;
}

最佳答案

我认为您可能有太多包装器,但这是一种结构稍微简单一些的方法

JSfiddle

HTML

<div class="thumbnail" data-content="Text here">
    <a href="site1.html"><img src="http://lorempixel.com/output/people-q-c-200-200-5.jpg"/></a>
</div>

<div class="thumbnail" data-content="Text here">
    <a href="site2.html"><img src="http://lorempixel.com/output/transport-q-c-200-200-6.jpg"/></a>
</div>>

CSS

.thumbnail {
    display: inline-block;
    position: relative;
}

.thumbnail a,
.thumbnail a img {
    display: block;
}

.thumbnail:after {
    content: attr(data-content);
    color:transparent;
    position:absolute;
    width:100%; height:100%;
    top:0; 
    left:0;
    background:rgba(0,0,0,0.0);
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}
.thumbnail:hover:after {
    background:rgba(0,0,0,0.6);
    color:#fff;
}

关于html - 如何在悬停多个缩略图图像时设置黑色透明度,每个图像都有不同的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23363563/

相关文章:

javascript - jquery - 选择点击宽度上的最后一个 div

css - 单选按钮不起作用。将选择但不选择背景颜色

javascript - 使图像在屏幕上显示一半,另一半滚动查看

html - CSS 图像 :after not working on Coppermine

javascript - 表格内容根据下拉选项而变化

html - 如何在显示悬停时隐藏/取消隐藏元素

php - 阻止网站运行数据库中的 JavaScript 代码

html - 使内部表格与溢出的 div 中的内容一样宽

html - 如何触发 HTML (SVG) 中的底层悬停事件?

html - 标签和 img 标签上的标题 seo 提示