【发布时间】:2015-01-12 12:56:49
【问题描述】:
标题说明了一切。如何使用 JQuery 和 .fadeIn 效果在悬停时交换图像?
我有一个标签,里面有一张黑白图像。当您将鼠标悬停在链接上时,我希望将黑白图像换成彩色版本并淡入。我正在寻找与本网站上的照片类似的效果:
http://www.manuelle-gautrand.com/
我的黑白图像的命名约定以 _bw 结尾,而我的彩色图像的名称相同,但没有 _bw。
这是我的 HTML:
<a href="link.html" class="item"><img src="images/cool_restaurant_bw.jpg" alt="Cool Restaurant" width="200" height="300"></a>
jQuery
$(document).ready(function() {
$(".item").hover( function() {
$(this).find("img").stop().fadeOut(0, function() {
$(this).find("img").attr("src", $(this).attr("src").replace("_bw", ""));}).fadeIn(400);
},
$(this).find("img").stop().fadeOut(0, function() {
$(this).find("img").attr("src", $(this).attr("src") + "_bw");}).fadeIn(0);
);
});
我的代码不起作用。有什么建议吗?
提前谢谢大家
【问题讨论】:
-
在这里:------>
$(this).attr("src").replace("_bw",),用什么替换"_bw"?您缺少一个参数。 -
你需要把它改成
$(this).attr("src").replace("_bw", "")
标签: jquery image replace hover swap