【问题标题】:How to swap images on hover using Jquery and .fadeIn effect?如何使用 Jquery 和 .fadeIn 效果在悬停时交换图像?
【发布时间】: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


【解决方案1】:

除非您使用图像预加载器,否则在更改 src 时会遇到加载问题。

最好加载所有图片(注意顶部图片上的display:none;)。

然后使用 jQuery toggle()fadeIn() fadeOut() 函数。
如果您不想在悬停开始时出现白色闪烁(与您发布的链接相同),那么您可以删除 toggle() 语句。

$(".item").hover(
  function() {
    // fade in
    $(this).find('.bottom-image').toggle(0);
    $(this).find('.top-image').fadeIn(500);
  }, function() {
    //fade out
    $(this).find('.bottom-image').toggle(0);
    $(this).find('.top-image').fadeOut(500);
  }
);
.bottom-image,
.top-image {
  position: absolute;
}

.bottom-image {
  background-color: #777;
}
.top-image {
  display: none;
  background-color: #33a;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<a href="link.html" class="item">
  <img src="images/cool_restaurant_bw.jpg" alt="Cool Restaurant" width="200" height="300" class="bottom-image">
  <img src="images/cool_restaurant.jpg" alt="Cool Restaurant" width="200" height="300" class="top-image">
</a>

【讨论】:

  • 谢谢蒂姆斯。太棒了。我不认为我可以使用 position:absolute 使用两个图像来完成此操作,但我找到了一种使其工作的方法。太棒了。感谢您的帮助。
【解决方案2】:

我会做一些不同的事情,那就是让彩色图像成为链接的背景图像,而不是交换来源。如果您交换源,它将无法从一个图像淡入另一个图像 - 为此,它们必须同时存在。

相反,我会添加一个小函数来遍历所有图像并将彩色图像设置为背景图像:

$('.item').each(
    function() {
        var colorbg = $(this).find('img').attr('src').replace("_bw", "");
        $(this).css('background', 'url(' + colorbg + ')');
    }
);

您还必须在链接中添加一些 CSS:

.item {
    display: block;
    height: 300px;
    width: 200px;
}

然后您可以为图像设置动画以在悬停时按照您想要的方式淡入和淡出:

$('.item').hover(
    function() {
        $(this).stop().animate({"opacity": "0"}, 0);
        $(this).find('img').stop().animate({"opacity": "0"}, 0);
        $(this).stop().animate({"opacity": "1"}, 500);
    },
    function() {
        $(this).find('img').stop().animate({"opacity": "1"}, 0);
    }
);

(上面的函数首先使链接和图像“不可见”,然后淡入链接,即彩色图像。)

这是一个小提琴(使用不同的图片来源):http://jsfiddle.net/Niffler/Lvpx9h3j/

【讨论】:

  • 感谢 Niffler 的回答。我正在寻找原始图像消失然后新图像快速出现在同一位置的整体效果。我编辑了 OG 帖子,其中包含指向我要复制的效果的链接。
  • @NicholasSmith 我已经更新了脚本,使其与您发布的链接具有相同的效果。你介意检查一下小提琴是否是你的意思吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-04
  • 1970-01-01
相关资源
最近更新 更多