Home > Web Front-end > JS Tutorial > body text

Sharing of various js image preloading implementation methods_javascript skills

WBOY
Release: 2016-05-16 15:14:44
Original
1344 people have browsed it

There are generally several ways to preload images

1.html tag or css loading image

Obviously we can preload images using the img tag or through the background-image attribute of the tag. But in order to avoid loading too many pictures for the first time and affecting the experience. It is generally best to load the document after the document is rendered (using window.onload, etc.).

2. Pure js to implement preloading

The Empty City Plan-Code’s Javascript implements the preloading of images. The complete preloading example is

function preloadimages(arr){  
  var newimages=[], loadedimages=0
  var postaction=function(){} //此处增加了一个postaction函数
  var arr=(typeof arr!="object")? [arr] : arr
  function imageloadpost(){
    loadedimages++
    if (loadedimages==arr.length){
      postaction(newimages) //加载完成用我们调用postaction函数并将newimages数组做为参数传递进去
    }
  }
  for (var i=0; i<arr.length; i++){
    newimages[i]=new Image()
    newimages[i].src=arr[i]
    newimages[i].onload=function(){
      imageloadpost()
    }
    newimages[i].onerror=function(){
      imageloadpost()
    }
  }
  return { //此处返回一个空白对象的done方法
    done:function(f){
      postaction=f || postaction
    }
  }
}
Copy after login

The principle is to create an Image object in a loop, set the src of the object to the specified image, and then monitor the image loading completion onload = function(){imageloadpost()}. When the image loading is completed, imageloadpost will be executed. It turns out that IE6 still has a problem: if the preloaded image is already in memory, the img.onload event will not be triggered again. But IE7+ has no problem. There is no problem with other browsers, so the above img.onload listening event has no compatibility issues.

3.Ajax implements preloading

Ajax requests can request any data, and pictures are no exception. Let’s take a look at js/css preloading

// XHR to request a JS and a CSS 
    var xhr = new XMLHttpRequest(); 
    xhr.open('GET', 'http://domain.tld/preload.js'); 
    xhr.send(''); 
    xhr = new XMLHttpRequest(); 
    xhr.open('GET', 'http://domain.tld/preload.css'); 
    xhr.send(''); 
Copy after login

The ajax preloading of images is actually the same as the pure js preloading of images

new Image().src = "http://domain.tld/preload.png"; 
Copy after login

It’s just that the explanation here is ajax loading. It can be understood that new images are all ajax get requests.

The above is the entire content of this article. I hope it will be helpful for everyone to understand js image preloading.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!