I have a page with four images side by side. What I would like to do is use jquery to fade out the current image and fade in a new one as a hover effect on each image. I didn’t want to do it with just plain css and image sprites because I wanted a smoother effect.
I tried using this code. I could get the images to switch but couldn’t figure out how to add the fade in and fade out to the images.
Thanks
jQuery(document).ready(function($) {
//rollover swap images with rel
var img_src = "";
var new_src = "";
$(".rollover").hover(function(){
//mouseover
img_src = $(this).attr('src'); //grab original image
new_src = $(this).attr('rel'); //grab rollover image
$(this).attr('src', new_src); //swap images
$(this).attr('rel', img_src); //swap images
},
function(){
//mouse out
$(this).attr('src', img_src); //swap images
$(this).attr('rel', new_src); //swap images
});
//preload images
var cache = new Array();
//cycle through all rollover elements and add rollover img src to cache array
$(".rollover").each(function(){
var cacheImage = document.createElement('img');
cacheImage.src = $(this).attr('rel');
cache.push(cacheImage);
});
});
To do a fade, you have to create a second image and stack them on top of each other (with position: absolute or the like). Then do the fade. You can then add a timeout to remove the other image once the fade is complete.
Here is what I came up with. I can’t takle credit though, found the answer through google.
Demo: http://waldrondigitaldesigns.com/gallery.html
Jquery:
// wrap as a jQuery plugin and pass jQuery in to our anoymous function
(function ($) {
$.fn.cross = function (options) {
return this.each(function (i) {
// cache the copy of jQuery(this) - the start image
var $$ = $(this);
// get the target from the backgroundImage + regexp
var target = $$.css('backgroundImage').replace(/^url|[\\(\\)'"]/g, '');
// nice long chain: wrap img element in span
$$.wrap('<span style="position: relative;"></span>')
// change selector to parent - i.e. newly created span
.parent()
// prepend a new image inside the span
.prepend('<img>')
// change the selector to the newly created image
.find(':first-child')
// set the image to the target
.attr('src', target);
// the CSS styling of the start image needs to be handled
// differently for different browsers
if ($.browser.msie || $.browser.mozilla) {
$$.css({
'position' : 'absolute',
'left' : 0,
'background' : '',
'top' : this.offsetTop
});
} else if ($.browser.opera && $.browser.version < 9.5) {
// Browser sniffing is bad - however opera < 9.5 has a render bug
// so this is required to get around it we can't apply the 'top' : 0
// separately because Mozilla strips the style set originally somehow...
$$.css({
'position' : 'absolute',
'left' : 0,
'background' : '',
'top' : "0"
});
} else { // Safari
$$.css({
'position' : 'absolute',
'left' : 0,
'background' : ''
});
}
// similar effect as single image technique, except using .animate
// which will handle the fading up from the right opacity for us
$$.hover(function () {
$$.stop().animate({
opacity: 0
}, 450);
}, function () {
$$.stop().animate({
opacity: 1
}, 450);
});
});
};
})(jQuery);
// note that this uses the .bind('load') on the window object, rather than $(document).ready()
// because .ready() fires before the images have loaded, but we need to fire *after* because
// our code relies on the dimensions of the images already in place.
$(window).bind('load', function () {
$('img.fade').cross();
});
//-->
HTML
<div id="gallery">
<a href="engagements.html"><img src="Images/engagement-small.jpg" style="background: url(Images/engagement2-small.jpg);" alt="link to engagement gallery" class="fade" /></a>
<a href="weddings.html"><img src="Images/wedding-small.jpg" style="background: url(Images/wedding2-small.jpg);" alt="link to wedding gallery" class="fade" /></a>
<a href="weddings.html"><img src="Images/family-small.jpg" style="background: url(Images/family2-small.jpg);" alt="link to wedding gallery" class="fade" /></a>
<a href="weddings.html"><img src="Images/senior-small.jpg" style="background: url(Images/senior2-small.jpg);" alt="link to wedding gallery" class="fade" /></a>
</div>