简体   繁体   English

交叉淡入淡出图像到背景

[英]cross fade image to background

I'm looking to a simple crossfadding backround image. 我正在寻找一个简单的交叉淡化背景图像。

I have a initial background image and when the user click in the other thumbs the background image switch with a fadeIn to the new image 我有一个初始的背景图像,当用户用另一只拇指单击时,背景图像会随着淡入淡出而切换到新图像

I have this code: 我有以下代码:

$('#backgroundDiv img').fadeOut(550,'linear', function(){
    $('#backgroundDiv img').attr({'src':img_fs});
     //if(this.complete)
    $('#backgroundDiv img').fadeIn(250,'linear');
});

but it first makes a fadeOut and after a fadeIn. 但它首先会进行一次淡出,然后进行淡入。 I don't want a fadeOut because I don't want to see the background color. 我不想淡出,因为我不想看到背景色。 I comment one line because in IE9 and Chrome it crashes. 我评论一行,因为在IE9和Chrome中它崩溃了。

Can anyone help me with any cross fadding?? 谁能帮助我完成任何交叉淡化?

Thanks 谢谢

So what you want is fading out an image, and fading in another one that is virtually "under" the first one, right ? 因此,您想要的是淡出图像,然后淡入实际上在第一个图像“下方”的另一个图像,对吗?

Your code waits for the first image to disappear before fading in the other one. 您的代码等待第一个图像消失,然后在另一个图像中消失。

What i suggest is that you do both actions at the same time, so that when the first image starts to disappear, the second one already started to appear : 我建议您同时执行两个操作,以便当第一个图像开始消失时,第二个图像已经开始出现:

$('#backgroundDiv img').fadeOut(550,'linear');
$('#apresentacaoBgImage img').attr({'src':img_fs}).fadeIn(250,'linear');

EDIT : If you only have one image, that's literally not possible. 编辑:如果只有一个图像,那实际上是不可能的。 So we have to create a copy of that image and do the stuff you want with those two ones, because at some point (when the first image starts to disappear and the second one starts to appear) we definitely need two images. 因此,我们必须创建该图像的副本,并对这两个图像进行所需的处理,因为在某个时候(当第一个图像开始消失而第二个图像开始出现时),我们肯定需要两个图像。 Here's what i did : 这是我所做的:

Note that the div #backgroundDiv must only contain one image, since it's used as position: relative to position the second image under the first one before all the fancy stuff. 请注意,div #backgroundDiv只能包含一个图像,因为它用作position: relative对于将第二个图像#backgroundDiv在所有奇特的东西之前的第一个图像之下

Working example here. 这里的工作示例。

HTML : HTML:

<div id="backgroundDiv" style="position: relative;">
    <img src="http://trollface.sparxified.com/Trollface_HD.jpg" width="200px" height="200px" />
</div>

JS : JS:

var img_fs = 'http://driph.com/words/wp-content/uploads/2008/04/e.gif';
$("#backgroundDiv img").clone().appendTo("#backgroundDiv").hide().attr({ src: img_fs }).css({ position: 'absolute', top: 0, left: 0 });
$("#backgroundDiv img:first").fadeOut(550,'linear');
$("#backgroundDiv img:last").fadeIn(550,'linear');

PS : i modified the duration of both fadeIn() and fadeOut() to be the same as it looked weird with different ones. PS:我修改了fadeIn()fadeOut()的持续时间,使其与使用不同的fadeIn()看起来很奇怪。

maybe this would help you 也许这会帮助你

$('#backgroundDiv img').hide()
$('#apresentacaoBgImage img').attr('src',img_fs).fadeIn(250,'linear');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM