Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to smoothly change <img src="..." /> with jQuery?

I'm now doing something like :

$img.hover(function(){$(this).attr('src','1.jpg')},function(){$(this).attr('src','2.jpg')});

Which is not smooth,because it takes quite some time to load an image.

like image 622
Mask Avatar asked Dec 13 '22 01:12

Mask


1 Answers

What about pre-loading your images when the page loads:

$(function () {
  var preloadImages = ['1.jpg', '2.jpg'];

  $.each(preloadImages, function () {
    $('<img/>').attr('src', this);
  });

  // ...
});
like image 137
Christian C. Salvadó Avatar answered Dec 23 '22 19:12

Christian C. Salvadó