I'm trying to figure out how to correctly hover over an image to fade in another image and when you move your mouse out, it would go fade back to the normal image.
$(document).ready(function(){
$('img').hover(
function(){
$(this).stop().fadeOut(function(){
var $temp = $(this).attr('src');
$(this).attr('src', $(this).attr('data-alt-src'));
$(this).attr('data-alt-src', $temp);
});
$(this).fadeIn();
},
function(){
$(this).stop().fadeOut(function(){
var $temp = $(this).attr('data-alt-src');
$(this).attr('data-alt-src', $(this).attr('src'));
$(this).attr('src', $temp);
});
$(this).fadeIn();
}
);
});
#main-wrapper {
width: 970px;
margin: 0 auto;
text-align: center;
}
img {
width: 200px;
}
<!DOCTYPE html>
<html>
<head>
<title>Hover In/Out</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="styles.css">
<script src= 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
<script src="script.js"></script>
</head>
<body>
<div id="main-wrapper">
<img src="frontside.jpg" data-alt-src="backside.jpg" alt="front side picture">
<img src="frontside.jpg" data-alt-src="backside.jpg" alt="front side picture">
<img src="frontside.jpg" data-alt-src="backside.jpg" alt="front side picture">
<img src="frontside.jpg" data-alt-src="backside.jpg" alt="front side picture">
</div>
</body>
</html>
So far, it works as it should with one minor defect. If I were to quickly hover my mouse over the image and out, it would stay in the second image. I initially wanted to hover over the image to show the other image and then when I hover out from the image it would go back to its initial image.
div
container and setbackground-image
for it? \$\endgroup\$