如何在悬停时显示图片上的文字? [英] How to show text on image when hovering?

查看:117
本文介绍了如何在悬停时显示图片上的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将鼠标悬停在图片上时,我会尝试显示说明。我已经以不太理想的方式使用图片精灵和悬停在这里: http://willryan.us/design.html 我想让它看起来完全像我一样,但使用真实的文字而不是图像。



我尝试了几个不同的东西,但我可以似乎找出了怎么做。我试图使用HTML和CSS只是,但我不知道是否可能。



随意在我的代码,我会



 

code>< div id =projectlist>
< div id =buzzbutton>< a href =buzz.htmltitle =>< img src =content / assets / thumbnailails / transparent_150x150.pngalt = />< / a>< / div>

< div id =slinksterbutton>< a href =slinkster.htmltitle =>< img src =content / assets / thumbnailails / transparent_150x150.png alt =/>< / a>< / div>
< / div>

CSS

  div#projectlist {
width:770px;
margin:0 auto;
margin-top:20px;
margin-bottom:40px;
}

div#buzzbutton {
display:block;
float:left;
margin:2px;
background:url(content / assets / thumbnailails / design / buzz_sprite.jpg)0 0px no-repeat;
width:150px;
height:150px;
}

div#buzzbutton:hover {
background:url(content / assets / thumbnailails / design / buzz_sprite.jpg);
width:150px;
height:150px;
background-position:0 -150px;
}

div#slinksterbutton {
display:block;
float:left;
background:url(content / assets / thumbnailails / design / slinkster_sprite.jpg)0 0px no-repeat;
width:150px;
height:150px;
margin:2px;
}

div#slinksterbutton:hover {
background:url(content / assets / thumbnailails / design / slinkster_sprite.jpg);
width:150px;
height:150px;
background-position:0 -150px;
}


解决方案

将图片和悬停显示说明包含在与图片尺寸相同的div中。然后,使用一些CSS,将描述顺序设置为悬停该div。



  body {font:14px / 1.4 arial;}。imgWrap {position:relative; height:200px; width:257px;}。imgDescription {position:absolute; top:0; bottom:0; left:0; right:0;背景:rgba(29,106,154,0.72); color:#fff; visibility:hidden;不透明度:0; / *删除注释,如果你想要状态之间的渐进过渡-webkit-transition:visibility opacity 0.2s; * /}。imgWrap:hover .imgDescription {visibility:visible; opacity:1;}  

 < div class =imgWrap > < img src =whatever.jpgalt =polaroid/> < / div>  

p>

I'm trying to show a description when hovering over an image. I've already done it in a less than desirable way, using image sprites and hovers here: http://willryan.us/design.html I want it to look exactly like how I have it, but using real text instead of an image.

I've tried a few different things but I can't seem to figure out how to do it. I'm trying to do it using HTML and CSS only, but I'm not sure if that's possible.

Feel free to poke around in my code, I'll paste what I think is relavent here.

HTML

<div id="projectlist">
                <div id="buzzbutton"><a  href="buzz.html" title=""><img src="content/assets/thumbnails/transparent_150x150.png" alt="" /></a></div>

                <div id="slinksterbutton"><a href="slinkster.html" title=""><img src="content/assets/thumbnails/transparent_150x150.png" alt="" /></a></div>             
</div>

CSS

div#projectlist {
    width: 770px;
    margin:0 auto;
    margin-top:20px;
    margin-bottom:40px;
}

div#buzzbutton {
        display:block;
        float:left;
        margin:2px;
        background: url(content/assets/thumbnails/design/buzz_sprite.jpg) 0 0px no-repeat;
        width: 150px;
        height: 150px;
    }

    div#buzzbutton:hover {
        background:url(content/assets/thumbnails/design/buzz_sprite.jpg);
        width:150px;
        height:150px;
        background-position:0 -150px;
    }

div#slinksterbutton {
        display:block;
        float:left;
        background:url(content/assets/thumbnails/design/slinkster_sprite.jpg) 0 0px no-repeat;
        width: 150px;
        height: 150px;
        margin:2px;
    }

    div#slinksterbutton:hover {
        background:url(content/assets/thumbnails/design/slinkster_sprite.jpg);
        width:150px;
        height:150px;
        background-position:0 -150px;
    }

解决方案

It's simple. Wrap the image and the "appear on hover" description in a div with the same dimensions of the image. Then, with some css, order the description to appear while hovering that div.

body {
  font: 14px/1.4 arial;
}
.imgWrap {
  position: relative;
  height: 200px;
  width: 257px;
}
.imgDescription {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(29, 106, 154, 0.72);
  color: #fff;
  visibility: hidden;
  opacity: 0;
  /*remove comment if you want a gradual transition between states
      -webkit-transition: visibility opacity 0.2s;
      */
}
.imgWrap:hover .imgDescription {
  visibility: visible;
  opacity: 1;
}

<div class="imgWrap">
  <img src="whatever.jpg" alt="polaroid" />
  <p class="imgDescription">This image looks super neat.</p>
</div>

这篇关于如何在悬停时显示图片上的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆