I have a website with images on it. Some of the images are animated GIF images. What I've done is to show a static image of the GIF and on a click event, the actual GIF images is displayed.
Here is my code:
JQuery
$(".theimage").click(function() {
$(this).addClass('hidden');
$(this).next().removeClass('hidden');
});
$(".theimage2").click(function() {
$(this).addClass('hidden');
$(this).prev().removeClass('hidden');
});
HTML
<img class="theimage" src="image_static.gif" alt=""/>
<img class="theimage2 hidden" src="image_animated.gif" alt=""/>
CSS
.hidden{
display:none !important;
}
I am changing the class of the previous and next elements because I may have more than one GIF image on a single page, thus preventing the action of occurring for all of them.
So far so good, the script works. What I am trying to achieve actually is for the GIF image to always start from the beginning. At the moment it only does so the first time. Then, it looks like it continues playing in the background and on the second call, it simply continues playing. Sometimes even it freezes, something to do with cache I guess.
How can i accomplish this?