I have a very simple webpage that contains 4 color images. When I hover over any of the images the original (xxxx1.jpg) is replaced by a black and white version (xxxxx2.jpg) of the same image, and then it reverts back to the original image after hover.
I'm declaring the same variable for both handlerIn
and handlerOut
:
var src = $(this).attr("src")
Can I declare the var
just once somehow? I just can't seem to figure this out myself.
$(document).ready(function(){
$( "img" ).hover(
function() {
var src = $(this).attr("src").replace("1.jpg", "2.jpg");
$(this).attr("src", src);
},
function() {
var src = $(this).attr("src").replace("2.jpg", "1.jpg");
$(this).attr("src", src);
}
);
});
background-image
to set the image on element, on:hover
change the background image. \$\endgroup\$hover()
and CSS classes andtoggleClass
in callbacks. \$\endgroup\$