views:

641

answers:

3

Hi,

How do I animate between two PNG images in jQuery? Is this possible?

Something like the CSS 3 transition when you transit between one color to another, but I need an image to an image transition.

+1  A: 

This might help: http://snook.ca/archives/javascript/jquery-bg-image-animations/

Michael Bray
It does not animate between two images, all it does is that it moves one image over another image.
rFactor
+1  A: 

Check out this plugin by Jack Moore called jQuery Blend.

Edit: Opps two images, sorry. How about this plugin then?


Ok, if you aren't happy with a plugin then try this. I posted a demo here.

CSS/HTML

<style type="text/css">
.wrap {
 margin: 50px auto;
 width: 200px;
 height: 200px;
 background: #555;
 position: relative;
}
.display1, .display2 {
 position: absolute;
 top: 0;
 left: 0;
}
</style>
<div class="wrap">
 <div class="display1"></div>
 <div class="display2"></div>
</div>

Script

$(document).ready(function(){
 var bkgImgs = ([
  ['http://i50.tinypic.com/2iiz9cm.gif', 86, 86],
  ['http://i45.tinypic.com/dop26e.png', 128, 128],
  ['http://i48.tinypic.com/xcosnq.png', 64, 64]
 ]);
 var delay = 5000;
 var transition = 1000;

 var i = 0;
 var l = bkgImgs.length-1;
 imgs = function(x){
  return {
   background: 'url(' + bkgImgs[x][0] + ') no-repeat center center',
   width:      bkgImgs[x][1],
   height:     bkgImgs[x][2],
   left:       $('.wrap').width()/2 - bkgImgs[x][1]/2,
   top:        $('.wrap').height()/2 - bkgImgs[x][2]/2
  }
 }
 next = function(){
  var oldpic = imgs(i);
  i++;
  if (i > l) i = 0;
  var newpic = imgs(i);
  $('.display2').css(oldpic).show();
  $('.display1').hide().css(newpic).fadeIn(transition);
  $('.display2').fadeOut(transition);
  setTimeout( function(){ next() }, delay );
 }
 next();
})
fudgey
It does not blend between two images. It has an image below a solid color layer that it uses to make this "blend" animation.
rFactor
Opps, for some reason I didn't read "two images"
fudgey
I added some code... it does pretty much what Jordan describes.
fudgey
Thanks, it's certainly of useful. I'm wondering if I should now take the path and use this hackish method to achieve the result or just wait for an official CSS/jQuery implementation.
rFactor
What do you mean by "official"? And how is the code I provided hackish?
fudgey
I imagine by "official" they actually mean a full-on jQuery plugin that looks something closer to $("selector").animateBackgrounds({action: 'switch',backgrounds: ['bg1.png','bg2.png','bg3.png'],effect: 'dissolve'})Unlikely to be built anytime soon since I would imagine this is a fairly unusual request. As far as "hackish" I would actually agree with him to the extent that this code doesn't have the normal clean look that most jQuery does, but that's mostly because so much of the functionality has to be handcoded rather than using already extant jQuery functions.
Jordan Reiter
The only change I might make to your code is to have jQuery create the display1 and display2 divs itself. Also, I haven't tested it but I'm not sure that as it stands you could actually have content in in the .wrap div and have display1 and display2 show behind it, since I'm fairly sure you'd need to set the z-Index as well. But major kudus for actually implementing the thing!
Jordan Reiter
Hi Jordan, thanks for the feedback. I thought about having the code create the display1 and display2, but I figured it would just make the code that much longer *shrug*. I'd modify it, but it doesn't seem like the original poster cares anymore? Also, since the display2 is after display1 in the HTML, it will always be above display1, so I didn't bother adding a z-index.
fudgey
+1  A: 

When you say "Animate between two images" do you mean you want them to fade into one another?

I think what you would have to do is basically create two divs that float underneath the main content (using z-index) and then fade between them by:

  1. putting image B (hidden) behind image A (say, by setting image B's z-index to 10 and image A z-index to 20)

  2. showing image B using .show() [it would still be hidden behind A]

  3. Fading out image A using .fadeOut()

repeat 1-3 switch A and B

If you want this animation to be ongoing, you could use window.setInterval() to run the code every so often. You could have a variable current_bg that stores either A or B to keep track of which way you should make the switch.

Jordan Reiter
Ok. Do you think that there's gonna be a jQuery implementation or a CSS implementation that allows image-to-image animations in the future? I could just wait for it... or make a hackish attempt to achieve it now.
rFactor