Show title tag on hover inside img

荒凉一梦 提交于 2021-01-28 21:05:16

问题


Hi I need show a Title tag of img inside img.

Like this example http://jsfiddle.net/51csqs0b/

.image {
    position:relative;
    width:200px;
    height:200px;
}
.image img {
    width:100%;
    vertical-align:top;
}
.image:after, .image:before {
    position:absolute;
    opacity:0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}
.image:after {
    content:'\A';
    width:100%; height:100%;
    top:0; left:0;
    background:rgba(0,0,0,0.6);
}
.image:before {
    content: attr(data-content);
    width:100%;
    color:#fff;
    z-index:1;
    bottom:0;
    padding:4px 10px;
    text-align:center;
    background:red;
    box-sizing:border-box;
    -moz-box-sizing:border-box;
}
.image:hover:after, .image:hover:before {
    opacity:1;
}
<div data-content="Here is a caption" class="image">
    <img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

<hr>
    
<div data-content="Different caption here..." class="image">
    <img src="http://i.stack.imgur.com/Sjsbh.jpg" alt="" />
</div>

But i don't want use a div before img.

<a href="#"><img class="" title="TITLE NEED SHOW ON HOVER" src=""/></a>

It's possible to show up? Thanks


回答1:


This is all thing I can do:

add .image class to a

<a class="image" href="#"><img src="http://i.stack.imgur.com/Sjsbh.jpg" class="" title="TITLE NEED SHOW ON HOVER" src=""/></a>

replace data-content with title in css:

.image:before {
    content: attr(title);
    width:100%;
    color:#fff;
    z-index:1;
    bottom:0;
    padding:4px 10px;
    text-align:center;
    background:red;
    box-sizing:border-box;
    -moz-box-sizing:border-box;
}

finally, add this jQuery code:

$("a.image").each(function(){$(this).attr('title',$(this).find("img").attr('title'))});

Run it yourself here:




回答2:


Here is an easier way. You just use :after pseudoelement on hover. Pure CSS solution

<a href="" class="img"></a>

.img{
  display: block;
  width:200px;
  height:200px;
  background: url('http://i.stack.imgur.com/Sjsbh.jpg');
  background-size: 200px 200px;
  position: relative;
}
.img:hover::after {
  content:'This is a description';
  position: absolute;
  width:100%; 
  height:20%;
  bottom:0; left:0;
  background: rgba(255, 255, 255, 0.8);
 }

You won't very specific. I hope it will help. You can also make a tooltip.



来源:https://stackoverflow.com/questions/40039330/show-title-tag-on-hover-inside-img

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!