jQuery implements the method of preloading images

  • 2020-05-16 06:24:55
  • OfStack

This article illustrates an example of how jQuery implements preloading images. Share with you for your reference. The specific analysis is as follows:

This js code is used for preloading images, remember it is preloading, not post-loading, or loading images into the user's browser cache before they are displayed, so that they can be displayed very quickly, without having to go to the server to read the images.


jQuery.preloadImages = function() { 
  for(var i = 0; i < arguments.length; i++) { 
    $("<img />").attr('src', arguments); 
  }
};
// usage  
$.preloadImages('image1.gif','/path/to/image2.png','some/image3.jpg');

I hope this article is helpful for you to design jQuery program.


Related articles: