jruzafa
8/28/2014 - 9:08 AM

change src image on hover

change src image on hover

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){
// Change the image of hoverable images
$("img.imgHoverable").hover( function() {
var hoverImg = HoverImgOf($(this).attr("src"));
$(this).attr("src", hoverImg);
}, function() {
var normalImg = NormalImgOf($(this).attr("src"));
$(this).attr("src", normalImg);
}
);

function HoverImgOf(filename)
{
 var re = new RegExp("(.+)\\.(gif|png|jpg)", "g");
 return filename.replace(re, "$1_hover.$2");
}
function NormalImgOf(filename)
{
 var re = new RegExp("(.+)_hover\\.(gif|png|jpg)", "g");
 return filename.replace(re, "$1.$2");
}

}); 

</script>