最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make text appear on top of the image when hovering? - Stack Overflow

programmeradmin0浏览0评论

When a mouse hovering over the image, the image will be blurred and there will be text showing up on top of the image. I tried it myself using the code below but it appeared that the "text" move outside the image when hovering ...can anyone tell me why?

Code:

Html:

<span class ="row_1">
<a href="#">
<div class = "caption"> testing </div>
<img class = "img_link" src="image/food/food1.jpg" />
</a>
</span>

CSS:

.caption
{
display: none;
}

Jquery:

    $('a').hover(
    function(){
    var image = $(this).find('img'),
     caption = $(this).find('div');
     caption.width(image.width());
     caption.height(image.height());
     caption.fadeIn();
    },
    function(){
     var image= $(this).find('img'),
        caption = $(this).find('div');

    caption.width(image.width());
    caption.height(image.height());
    caption.fadeOut();
});

When a mouse hovering over the image, the image will be blurred and there will be text showing up on top of the image. I tried it myself using the code below but it appeared that the "text" move outside the image when hovering ...can anyone tell me why?

Code:

Html:

<span class ="row_1">
<a href="#">
<div class = "caption"> testing </div>
<img class = "img_link" src="image/food/food1.jpg" />
</a>
</span>

CSS:

.caption
{
display: none;
}

Jquery:

    $('a').hover(
    function(){
    var image = $(this).find('img'),
     caption = $(this).find('div');
     caption.width(image.width());
     caption.height(image.height());
     caption.fadeIn();
    },
    function(){
     var image= $(this).find('img'),
        caption = $(this).find('div');

    caption.width(image.width());
    caption.height(image.height());
    caption.fadeOut();
});
Share Improve this question edited Apr 16, 2012 at 20:07 qwr qwr asked Apr 16, 2012 at 20:00 qwr qwrqwr qwr 1,0594 gold badges22 silver badges46 bronze badges 16
  • "test was out of the box" ? what do you mean ? what is it that you exactly want ? – Dhiraj Commented Apr 16, 2012 at 20:02
  • "but didn't work" ohhh, that just says it all! not... :( – gdoron Commented Apr 16, 2012 at 20:03
  • There shouldn't be a space between the attribute and the value, <div class="testing"> for example – Dale Commented Apr 16, 2012 at 20:03
  • 3 Do you want this jsfiddle/heera/6v8Tb – The Alpha Commented Apr 16, 2012 at 20:13
  • 1 @SheikhHeera, I hope you dont mind me using your fiddle in my demo. ;) – Starx Commented Apr 16, 2012 at 20:48
 |  Show 11 more ments

2 Answers 2

Reset to default 6

First off, I had to correct your HTML. A div (block-level element) is not a valid child of either a span or a elements (both of which are in-line elements). So, amended your HTML to the following:

<span class="row_1">
    <a href="#">
        <span class="caption">testing</span>
        <img class="img_link" src="http://davidrhysthomas.co.uk/img/dexter.png" />
    </a>
</span>​

That said, I'd suggest, if possible, using plain CSS for this:

a {
    display: inline-block;
    position: relative;
}
.caption {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    background-color: #333; /* for browsers that don't understand rgba() notation */
    background-color: rgba(0,0,0,0.6);
    color: #f90;
    font-weight: bold;
    line-height: 1.1em;
}
a:hover .caption {
    display: block;
}​

JS Fiddle demo.

You can, with CSS3 transitions, even implement the fade-in transition as well (which gracefully degrades for those browsers that don't understand/implement transitions, albeit in this example you might have to use a Microsoft proprietary filter for older-IE pliance):

a {
    display: inline-block;
    position: relative;
}
.caption {
    opacity: 0;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    background-color: #333; /* for browsers that don't understand rgba() notation */
    background-color: rgba(0,0,0,0.6);
    color: #f90;
    font-weight: bold;
    line-height: 1.1em;
    -webkit-transition: all 1s linear;
    -o-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -moz-transition: all 1s linear;
    transition: all 1s linear;
}
a:hover .caption {
    opacity: 1;
    -webkit-transition: all 1s linear;
    -o-transition: all 1s linear;
    -ms-transition: all 1s linear;
    -moz-transition: all 1s linear;
    transition: all 1s linear;
}​

JS Fiddle demo.

If you must use jQuery then I'd suggest keeping it very, very simple:

​$('.row_1 a').hover(
    function(){
        $(this).find('.caption').fadeIn(1000);
    },
    function(){
        $(this).find('.caption').fadeOut(1000);
    });​​​​​​​​

JS Fiddle demo.

References:

  • CSS transition.
  • fadeIn().
  • fadeOut().
  • find().

You dont need Javascript for this. This snippet below is enough to bring up the caption.

a:hover .caption { display: block; }

But the caption has to be positioned correctly first.

Demo

发布评论

评论列表(0)

  1. 暂无评论