简体   繁体   English

在图像上的鼠标悬停时显示共享图标

[英]Show a sharing icon on hover on image

I have an image like this : 我有这样的图像:

<img id="item_img" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRH7oGdODLKgm4gaTYHdWDPYLUASSrLYxW4-YDuaZz-iU8jx5_3jg" alt="" height="333px" width="500px" />

With css : 使用css:

#item_img:hover {
  opacity: 0.4;
}

With this code, i apply a little opacity on hover. 有了这段代码,我对悬停应用了一些不透明性。 Also, I would like to show in the middle of the image, this sharing icon when the hover is active : 此外,当悬停处于活动状态时,我想在图像中间显示此共享图标:

在此处输入图片说明

How can i do that ? 我怎样才能做到这一点 ?

You can use following; 您可以使用以下内容;

HTML: HTML:

<div class="image-div">
    <img src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcRH7oGdODLKgm4gaTYHdWDPYLUASSrLYxW4-YDuaZz-iU8jx5_3jg" />
    <img class="hidden_img" src="http://i.stack.imgur.com/P1ELC.png" width="30" height="30" />
</div>

CSS: CSS:

.image-div {
    position: relative;
    float:left;
    margin:5px;}

.image-div:hover .hidden_img
  {
  display: block;
  }

.image-div .hidden_img {
    position:absolute;
    top:0;
    left:0;
    display:none;
}

Here is a working demo: jsfiddle 这是一个工作示例: jsfiddle

You could do this using the css psuedo class after . 您可以after使用css psuedo类来执行此操作。 By using after to achive this you don't have to actually add the hover image in the HTML code. 通过使用after来实现此目的,您无需在HTML代码中实际添加悬停图像。

 .img-div{position:relative; display: inline-block;} .img-div:hover:after{ content:''; position:absolute; left: 0px; top: 0px; bottom: 0px; width: 100%; background: url('http://i.stack.imgur.com/P1ELC.png') center no-repeat; background-size: 50px; } .img-div:hover img{opacity: 0.4;} 
 <div class="img-div"> <img src="http://placehold.it/150x150"/> </div> 

FIDDLE 小提琴

HTML 的HTML

<a href="#" class="wrapper">
        <img class="share" src="http://i.stack.imgur.com/P1ELC.png">
        <img src="http://lorempixel.com/100/100">
</a>

CSS 的CSS

.wrapper {
    position: relative;
    padding: 0;
    width:100px;
    display:block;
    }
.share {
    position: absolute;
    color:#f00;
    background-color:rgba(255,255,255,0.8);
    width: 100px;
    height: 100px;
    line-height:100px;
    text-align: center;
    z-index: 10;
    opacity: 0;
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -o-transition: all 0.5s ease;
    transition: all 0.5s ease;
}
.share:hover {
    opacity:1;
}


img {
    z-index:1;
}

CodePen 密码笔

You can do something like: 您可以执行以下操作:

HTML 的HTML

<div class="outer-div">
    <img src="someimage.jpg" />
    <img class="hover-img" src="http://i.stack.imgur.com/P1ELC.png" width="30" height="30" />
</div>

CSS 的CSS

.outer-div {
    position: relative;
    float:left;
    margin:5px;}

.outer-div:hover .hover-img
  {
  display: block;
  }

.outer-div .hover-img {
    position:absolute;
    top:0;
    left:0;
    bottom:0;
    right:0;
    margin:auto;
    display:none;
}

This will show you the hover icon in center of your Image 这将在图像中心向您显示悬停图标

Or make it even simpler! 或使其更简单! http://jsfiddle.net/oneeezy/5bDKz/2/ http://jsfiddle.net/oneeezy/5bDKz/2/

For this you would change my background: black; 为此,您将更改我的背景:黑色; to background: url("your-image.jpg") no-repeat 0 0; 到背景:url(“ your-image.jpg”)不重复0 0;

    div { position: relative; overflow: hidden;  float: left; border-radius: 13em; }
    div img { width: 100px; height: 100px; float: left; position: relative; display: block; }

    /* Hover Effect */
    div:hover:after { content: ""; position: absolute; top: 50%; left: 50%; width: 25px; height: 25px; margin: -15px 0 0 -15px; background: black; border-radius: 3em; border: 2px solid white; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM