First off, as far as I remember the gif is only loaded once regardless of how many there are on the page (Just opened the dev tools on that page and it does. Thank you crystal ball.) So HTTP request overhead isn't a problem.
As for rendering, slower machines and browsers will tend to crawl when rendering a lot of animated gifs. So 40 of those would considerably be a pain to do.
Now off to your code, yes it is a problem because you are you are getting all .play
on every mouseenter
, create and append a new image object for each of them. The overhead lies in the creating of a jQuery object for the .play
set on each mouseenter
as well as the generation of an image for each .play
on each mouseenter
.
I think the JS solution is better because it doesn't render 40 animated gifs all at once, and your code can be tweaked a bit for optimization. We can create the image just once and move it around using appendTo
. Also, since the mouseover
handler already has a reference to the target being hovered, we can use that as target for appendTo
.
// Create an image just once
var img = $('<img src="http://media.giphy.com/media/5Vb7xQB7Z3ScE/giphy.gif">');
// Since we can't hover on .play because it's behind .img-circle, we designate
// .img-circle as our hover target, and use previousSibling to get the .play
$('.day').on('mouseover','.img-circle',function(event){
// Here, we are not generating any new objects. img and this.previousSibling
// are already provided for
img.appendTo(this.previousSibling);
});
Here's a working demo
.day
elements, do you mean that they are all identical, or are they slightly different? (If different, could you give an idea of how they vary?) \$\endgroup\$