I am trying to accomplish of the task of displaying text below an image when you hover over it. I do not want to use the title
attribute, because I want to be able style the text.
An example would be at Dribbble. When you hover over some of the pictures, the text PRO shows up next to the name of the person that posted the picture
Check out this quick JS FIDDLE.
Your HTML
<ul>
<li>
<div class="caption">this is my caption</div>
<img src='http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367'/>
</li>
<li>
<div class="caption">this is my caption</div>
<img src='http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367'/>
</li>
<li>
<div class="caption">this is my caption</div>
<img src='http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367'/>
</li>
<li>
<div class="caption"><span>this is my caption</span></div>
<img src='http://dribbble.com/system/users/22122/screenshots/244072/empirestate02_d_teaser.png?1314126367'/>
</li>
</ul>
Css
ul li{ float:left; padding:20px; border:solid gray 4px; margin:5px;}
ul li div{display:none; background:white; opacity:.5; position:absolute;}
and your javascript
$('ul li').mouseenter(function(){
var image= $(this).find('img'),
caption = $(this).find('div');
caption.width(image.width());
caption.height(image.height());
caption.fadeIn();
}).mouseleave(function(){
var image= $(this).find('img'),
caption = $(this).find('div');
caption.width(image.width());
caption.height(image.height());
caption.fadeOut();
});
This should give you an idea of how to achieve what your looking for. It obviously could be improved. Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With