【问题标题】:How to make a transparent overlay on image [duplicate]如何在图像上制作透明叠加层[重复]
【发布时间】:2014-12-16 08:08:39
【问题描述】:

我有一个这样的简单图像:

<img src="/assets/missing.png">

现在我想要的是能够将鼠标悬停在图像上并出现一个透明的黑色叠加层,上面有一个大的“x”。是否可以仅使用 CSS3 或者我需要 Javascript?如果是的话怎么办?


我有点让它工作,但我有一个我似乎无法解决的问题。截图如下:

Screenshot

如您所见,顶部缺少一个部分。这是我的CSS:

.image {
  padding: 0px;
  display: inline-block;
  vertical-align: middle;
  max-height: 150px;
  max-width: 150px;
  margin: 15px;
  position:relative;
}

.image img {
  width:100%;
  vertical-align:top;
}

.image:after {
  content:'\A';
  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;
}

【问题讨论】:

  • 这很可能使用 CSS :) 这是一个重复的问题 - Black transparent overlay on image hover with only CSS?
  • @misterManSam 谢谢它或多或少地工作,但我仍然有一个问题(检查问题编辑)。问题出在哪里?
  • 确保最大高度和宽度与图像尺寸相匹配。也就是说,I have optimised this version 通过结合一些答案。当您更改图像的最大图像宽度和高度时,这对您有何帮助?
  • @misterManSam 我想通了,这只是一个填充问题。感谢您的帮助:) 干杯
  • 不用担心,只是为了它,this version does not require a height and width。站点注释:请只接受对您有帮助的答案...您不需要接受任何答案 - 特别是如果您通过其他问题获得了大部分指导:)

标签: html css


【解决方案1】:

看看这个。

HTML:

<div class="div">
    <div class='overlay'><p>X</p></div>
    <img src="http://fc02.deviantart.net/fs71/f/2012/157/c/a/watch_dogs___wallpaper__2_by_danielskrzypon-d52hvi1.png"/>
</div>

CSS:

.div{
    height:400px;
    width:400px;
    float:left;
    cursor:pointer;
}
img{
    height:400px;
    width:400px;
}
.overlay{
    height:400px;
    width:400px;
    position:absolute;
    background:rgba(0,153,255,.8);
    text-align:center;
    display:none;
}
.overlay p{
    color:white;
    line-height:400px;
    font-size:80px;
    margin:0;
}

查询:

$(document).ready(function(){
    $(".div").stop().mouseenter(function(){
        $(".overlay").fadeIn('slow');
    });
    $(".div").stop().mouseleave(function(){
        $(".overlay").fadeOut('slow');
    });
})

看看。 JSFiddle:http://jsfiddle.net/u5ka7Lq4/19/

【讨论】:

    【解决方案2】:

    看看下面的小提琴。我希望它可以帮助。 http://jsfiddle.net/somy_taheri/93y6hwjk/1/

    <html>
        <div class="outer">
            <img src="http://placekitten.com/g/200/300">                    
                <div class="overlay">
                    <p class="text">x</p>
                </div>
        </div>
    </html>
    <style>
    .outer {
        position:relative;
        width: 200px;
        height: 300px;
    }
    .overlay {                      
        display: none;  
    }
    
    .outer:hover .overlay { 
        display: block;
        position: absolute;
        width: 100%;
        height: 100%;
        background: black;
        opacity: 0.7;
        top: 0;
    }
    .text {
        position: absolute;
        top: 50%;
        left: 50%;
        transform:translate(-50%, -50%);
        color:white;
    }
    </style> 
    

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 2016-03-03
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多