在img中悬停时显示标题标记

时间:2016-10-14 09:12:35

标签: php jquery html css wordpress

您好我需要在img。

中显示img的标题标签

就像这个例子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>

但我不想在img之前使用div。

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

有可能出现吗? 感谢

2 个答案:

答案 0 :(得分:1)

这就是我能做的一切:

.image课程添加到a

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

data-content替换为css中的title

.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;
}

最后,添加这个jQuery代码:

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

自己运行here

答案 1 :(得分:0)

这是一种更简单的方法。你只需在悬停时使用伪元素。 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);
 }

你不会非常具体。我希望它会有所帮助。 您也可以制作工具提示。

相关问题