Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show text when the cursor hovers over an image

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

like image 253
syrkull Avatar asked Nov 04 '22 15:11

syrkull


1 Answers

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.

like image 76
Amin Eshaq Avatar answered Nov 15 '22 01:11

Amin Eshaq