I have been searching online for awhile, trying to find the best way to write a jquery script that does this simple task: swapping an image on hover with an elegant fade effect. I have found many solutions (some way to cumbersome and clunky), and narrowed it down to what I see as the two best:
http://designwoop.com/2009/08/image-hover-effect-using-jquery-tutorial/
http://bavotasan.com/tutorials/creating-a-jquery-mouseover-fade-effect/
For my purposes, I want to be able to do this hover swap numerous times on one page. The page is http://www.vitaminjdesign.com. I currently have hovers on my "services nav" (different type of hover) but I want to apply them to all of the nav buttons, as well as the social icons in the footer. All of the hovers will be the same- two image files, one fading in to the other on hover, and returning on leave. What is the best possible way to go about this? One of the above links perhaps?
You could also accomplish this using a single background image, and fading in & out a transparent div. Here's a quick example, which could be extended to work automatically for a single class of image:
$(document).ready( function() {
$("#mask-div")
.css({
"position": "absolute",
"width": 275,
"height": 110,
"background": "rgba(255, 255, 255, 0.5)"
})
.mouseover( function() {
$(this).fadeOut("slow");
})
;
$("#test-img").mouseout( function() {
$("#mask-div").fadeIn("slow");
});
});
Running demo can be seen at jsbin: demo-one
UPDATE: Here is a more generic solution (incomplete, but shows some ideas): demo-two . Just add the class "fade-img" to any image you want to have this effect.
$(document).ready( function() {
$(".fade-img")
.before("<div class='fade-div'></div>")
.each( function() {
var img = $(this);
var prev = img.prev();
var pos = img.offset();
prev.css({ "height": img.height(), "width": img.width(), "top": pos.top, "left": pos.left })
.mouseover( function() {
$(this).fadeOut("slow");
}
);
})
.mouseout( function() {
$(this).prev().fadeIn("slow");
})
;
});