I'm looking to toggle between two images on mouseover/hover and stop the toggling when hovering stops.
Here is the HTML code I have:
<div class="fader">
<img src="image1" />
<img src="image2" />
</div>
This is the JQuery code I'm using:
$('.fader').hover(function() {
toggleImage();
});
function toggleImage() {
if($('.fader').is(":hover")) {
$('.fader').find("img").fadeToggle(1000);
toggleImage();
}
}
Some CSS for styling purposes:
.fader {
display: inline-block;
}
.fader img:last-child {
position: absolute;
top: 0;
left: 0;
display: none;
}
The issue I'm facing is that I can't get the toggleImage()
function to stop when the mouse is moved away or hovering stops.
I tried checking for :hover with the following code:
$('.fader').is(":hover")
But it didn't work as expected.
I need a way to stop the infinite loop of toggleImage()
on mouseout or when hovering stops. Any suggestions?
If you need more help, check out this JSFiddle link.