当鼠标悬停在图像上方时,图像将会模糊,并且图像顶部会显示文字。我自己尝试使用下面的代码,但看起来“文本”在悬停时移动到图像之外 ......谁能告诉我为什么?
代码:
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();
});
答案 0 :(得分:6)
首先,我必须更正您的HTML。 div
(块级元素)不是span
或a
元素的有效子元素(两者都是内嵌元素)。因此,将您的HTML修改为以下内容:
<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>
那就是说,如果可能的话,我建议使用普通的CSS:
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;
}
使用CSS3过渡,您甚至可以实现淡入过渡(对于那些不理解/实现过渡的浏览器,它会优雅地降级,尽管在此示例中您可能必须使用Microsoft专有过滤器-IE合规):
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;
}
如果你必须使用jQuery,那么我建议保持它非常非常简单:
$('.row_1 a').hover(
function(){
$(this).find('.caption').fadeIn(1000);
},
function(){
$(this).find('.caption').fadeOut(1000);
});
参考文献:
答案 1 :(得分:1)