简体   繁体   English

jquery图像淡入和淡出同时?

[英]jquery image fade-In and fade-Out at the same time?

I am trying to swap b/w few images using jquery. 我试图使用jquery交换b / w几个图像。 There are couple of ways to do so ... 有几种方法可以做到这一点......

  1. fadeOut current image and once its done then, fadeIn new image. fadeOut当前图像,然后一旦完成,fadeIn新图像。 (this is easy and i have been able to replicate it) (这很容易,我已经能够复制它)
  2. fadeOut current image and simultaneously fadeIn new image ... (the only solution i can think for this is to have two image tag, first fading Out current image and second fading in new image ) fadeOut当前图像并同时淡入新图像...(我能想到的唯一解决方案是拥有两个图像标记,首先淡出当前图像和新图像中的第二个淡入淡出)

Is there any better or different solution for the same .... ? 是否有相同的更好或不同的解决方案....?

It depends on your requirements. 这取决于您的要求。 If your images are of different sizes (aspect ratios), yes, the only solution is to stack two images on top of each other and fade in/out at the same time. 如果您的图像具有不同的大小(宽高比),是的,唯一的解决方案是将两个图像叠加在一起并同时淡入/淡出。

If your images have the same size, there's a nicer solution which I've used in the past. 如果您的图像大小相同,那么我过去使用的解决方案就更好了。 You take two nested <div> s: 你带两个嵌套的<div>

<div><div></div></div>

From CSS set their width/height to the width/height of your images and do the following from JavaScript: 从CSS设置它们的宽度/高度到图像的宽度/高度,并从JavaScript执行以下操作:

  1. Set the background image of the inner div to the first image 将内部div的背景图像设置为第一个图像
  2. Set the background image of the outer div to the second image 将外部div的背景图像设置为第二个图像
  3. Fade out the inner div (this will create a really nice and smooth transition effect between the two images); 淡出内部div(这将在两个图像之间创建一个非常漂亮和平滑的过渡效果);
  4. Set the background image of the inner div to the third image 将内部div的背景图像设置为第三个图像
  5. Fade in the inner div 淡入内在的div
  6. Set the background image of the outer div to the fourth image 将外部div的背景图像设置为第四个图像
  7. Fade out the inner div 淡出内部div
  8. etc... 等等...

This way, you're only fading one element (so it's fast), but you get a really nice effect. 这样,你只会淡化一个元素(所以它很快),但你会得到一个非常好的效果。 Make sure to preload your images for a seamless experience. 确保预加载图像以获得无缝体验。

Use Jquery.Cycle 使用Jquery.Cycle

Jquery.cycle can be set up like this Jquery.cycle可以像这样设置

$('.slides').cycle({
  fx:     'scrollHorz',
  speed:  400, 
  timeout: 0});

我很懒,只是使用jQuery Cycle Lite

$("#lazy").cycle();

Have both the images in same place with position attribute of css to absolute. 将两个图像放在同一位置,css的position属性为绝对值。 then try 然后试试

$('#image1').fadeOut('slow');
$('#image2').fadeIn('slow');

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

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