jQuery使用淡入淡出效果更改图像src

[英]jQuery Change Image src with Fade Effect


I'm trying to create an effect where you click on a thumbnail image, and the link to the thumbnail will replace the main image, but fade it in. This is the code I'm currently using:

我正在尝试创建一个效果,您单击缩略图图像,缩略图的链接将替换主图像,但淡入其中。这是我目前使用的代码:

$(".thumbs a").click(function(e) {
    e.preventDefault();
    $imgURL = $(this).attr("href");
    $(".boat_listing .mainGallery").fadeIn(400, function() {
        $(".boat_listing .mainGallery").attr('src',$imgURL);
    });
});

This works and replaces the image without a fade effect. What do I need to change to get the fadeIn effect to work?

这适用于替换图像而没有淡入淡出效果。我需要更改什么才能使fadeIn效果起作用?

4 个解决方案

#1


71  

You have to make it fadeOut() first, or hide it.

你必须首先使它成为fadeOut(),或隐藏它。

Try this :

尝试这个 :

$(".thumbs a").click(function(e) {
    e.preventDefault();
    $imgURL = $(this).attr("href");
    $(".boat_listing .mainGallery")
        .fadeOut(400, function() {
            $(".boat_listing .mainGallery").attr('src',$imgURL);
        })
        .fadeIn(400);
});

It should fadeOut the image, then change the src when it's hidden, and then fadeIn.

它应该淡出图像,然后在隐藏时更改src,然后淡入。

Here's a jsFiddle example.

这是一个jsFiddle示例。

#2


29  

I thing instead of using FadeIn and fadeOut, its better to use fadeTo functionality as fadeIn and fadeOut created a time gap between them for few micro-seconds.

我不是使用FadeIn和fadeOut,而是更好地使用fadeTo功能作为fadeIn和fadeOut在它们之间创建了一个时间间隔几微秒。

I have used above code from Sylvain : thanks to him

我使用了Sylvain的上述代码:感谢他

$("#link").click(function() {

  $("#image").fadeTo(1000,0.30, function() {
      $("#image").attr("src",$("#link").attr("href"));
  }).fadeTo(500,1);
  return false;
});

#3


0  

You cannot fade-in something that is already at 100% alpha :)

你不能淡入已经达到100%alpha的东西:)

In other words, you either fade it out or hide it, and then fade it in.

换句话说,你要么将其淡出或隐藏它,然后将其淡入。

I made this example, basically, I hide it and then fade it:

我做了这个例子,基本上,我隐藏它然后淡化它:

http://jsfiddle.net/uGFMt/

http://jsfiddle.net/uGFMt/

#4


0  

I reproduced the given samples above. It gives a strange flickering, which I found that it waits for image to load after it's opacity is restored to 1. I modified the code by Sandeep.

我复制了上面给出的样本。它给出了一个奇怪的闪烁,我发现它在不透明度恢复到1后等待图像加载。我修改了Sandeep的代码。

$("#link").click(function() {

$("#image").fadeTo(1000,0.30, function() {
  $("#image").attr("src",$("#link").attr("href"));
  $("#image").on('load', function(){
    $("#image").fadeTo(500,1);
  });
 });
 return false;
});`
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/05/12/7def853aed59d9bcfc9c44b4f4b91c1a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告