I have the following images:
Static Image | Starting Animation | Ending Animation
And I am using the following code (jsfiddle example):
HTML
<div id="link">
<div id="link-gif"> </div>
</div>
CSS
#link-gif {
width:150px;
height:40px;
border:1px solid #39464E;
background-image:url('https://i.stack.imgur.com/W4Kgi.png'); /*static*/
background-size:contain;
background-repeat:no-repeat;
background-position:bottom center;
cursor:pointer;
}
JS
$(function() {
$("#link-gif").hover(
function() {
/* starting animation */
$(this).css("background-image", "url('https://i.stack.imgur.com/14qMg.gif')");
},
function() {
/* ending animation */
$(this).css("background-image", "url('https://i.stack.imgur.com/npwJ0.gif')");
}
);
});
So in summary, what happens is: I first load the static image. When someone hover's over the #link-gif
, the starting animation gif starts, and when you hover off of it, the ending animation starts.
My real question is, how do I make sure these gif's are loaded before someone hovers over the image? The first time someone hovers, it is a little glitchy and then it get's better.
I did it this way so that it would work with the oldest browsers possible because I need at least IE8 support. If anyone has any better suggestions, I'm all for it.