Javascript to implement image pre-loading without waiting _ javascript tips-JS tutorial

Source: Internet
Author: User
Tags image flip
During website development, you often need to browse a large number of images on a certain page. javascript is used to implement an image browser, so that users do not have to wait too long to see other image websites. When developing, users often need to browse a large number of images on a page. If traffic is taken into account, as with pconline, only one image is displayed on each page, allowing users to download the entire page again every time they read an image. However, in the web era, more people are willing to use javascript to implement an image browser, allowing users to see other images without waiting too long.

After knowing the address of an image, you need to display it in a fixed html container (such as p, the most important thing is to know the width and height of the image to be displayed. Then, combine the width and height of the container to display the image according to a certain scaling ratio. Therefore, image pre-loading becomes the core function of the image browser.

Anyone who has done image flip knows that it is best to first download the image to a local place and cache it for the browser to avoid waiting during image rotation. In this case, the Image object in js is usually used. The general method is nothing more than this:

The Code is as follows:


Function preLoadImg (url ){
Var img = new Image ();
Img. src = url;
}


By calling the preLoadImg function and passing in the image url, you can download the image in advance. In fact, the pre-download function used here is basically the same as this. After the image is pre-downloaded, you can know the width and height of the image through the width and height attributes of img. However, when using the image browser, images are displayed in real time. For example, if you click the displayed button, the above Code will be called to load the image. Therefore, if you use img. width Directly, the image has not been completely downloaded. Therefore, some asynchronous methods are required to call the width and height of the img only after the image is downloaded.

In fact, it is not difficult to implement such an asynchronous method. The download event is also very simple, that is, a simple onload event. Therefore, we can write the following code:

The Code is as follows:


Function loadImage (url, callback ){
Var img = new Image ();
Img. src = url;
Img. onload = function () {// call the callback function asynchronously when the image is downloaded.
Callback. call (img); // switch the callback function this pointer to img.
};
}


Now, write a test case.

The Code is as follows:


Function imgLoaded (){
Alert (this. width );
}


Test it in firefox and find it is good. It is indeed the same as expected. After the image is downloaded, the image width will pop up. The results are the same no matter how many clicks or refreshes.

However, to do this, don't be too happy-you still need to consider the compatibility of the browser, so hurry to test it in ie. That's right. The image width is also displayed. However, when you click load again, the situation is different and no response is returned. Refresh the page.

After tests on multiple browser versions, we found that ie6 and opera both work like this, while firefox and safari work normally. In fact, the reason is quite simple, that is, the browser cache. After an image is loaded once, if a request is sent to the image again, the browser will not initiate a new request because the image has been cached, instead, it is directly loaded from the cache. For firefox and safari, their views make the two loading methods transparent to users, and will also cause image onload events, while ie and opera ignore this identity, it does not cause the onload event of images, so the above Code cannot achieve the effect in them.

What should we do? The best case is that the Image may have a status value indicating whether it has been loaded successfully. When loading from the cache, because you do not need to wait, this status value directly indicates that it has been downloaded. When loading from an http request, this value is not displayed because you need to wait for download. In this way, you can do it.

After some analysis, we finally found a property of the Image compatible with various browsers-complete. Therefore, make a judgment on this value before the image onload event. Finally, the Code becomes as follows:

The Code is as follows:


Function loadImage (url, callback ){
Var img = new Image (); // create an Image object to implement pre-download of the Image
Img. src = url;
If (img. complete) {// if the image already exists in the browser cache, call the callback function directly.
Callback. call (img );
Return; // return directly without handling the onload event
}
Img. onload = function () {// call the callback function asynchronously when the image is downloaded.
Callback. call (img); // replace this of the callback function with the Image object.
};
};


After so much effort, we finally made every browser meet our goal. Although the Code is very simple, it solves the most core problem in the image browser. Next you have to do the following:

The Code is as follows:






Js implements the action after the image is pre-loaded.





Script
Var I = 0;
Var c = 3;
Var imgarr = new Array
Imgarr [0] = "http://www.baidu.com/img/baidu_logo.gif ";
Imgarr [1] = "http://img.baidu.com/img/logo-img.gif ";
Imgarr [2] = "http://img.baidu.com/img/logo-zhidao.gif ";
Var Browser = new Object ();
Browser. userAgent = window. navigator. userAgent. toLowerCase ();
Browser. ie =/msie/. test (Browser. userAgent );
Browser. Moz =/gecko/. test (Browser. userAgent );
Function SImage (url, callback)
{
Var img = new Image ();
If (Browser. ie ){
Img. onreadystatechange = function (){
If (img. readyState = "complete" | img. readyState = "loaded "){
Ii = I + 1;
Callback (I );
}
}
} Else if (Browser. Moz ){
Img. onload = function (){
If (img. complete = true ){
Ii = I + 1;
Callback (I );
}
}
}
Img. src = url;
}
Function icall (v)
{
If (v SImage ("" + imgarr [v] + "", icall );
}
Else if (v> = c ){
I = 0;
// Location.replace('banner.html '); // write your own actions here,
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.