A large number of images with high resolution can indeed make a Web site radiant. But it will also cause the site access speed to decrease - if the picture is a file, the file will occupy bandwidth, and the bandwidth is directly related to the access waiting time. Now, let's learn a little trick called image preloading to improve image access speed.
Some browsers try to solve this problem by saving these images in a local cache. This allows these images to be called in sequence - but there is still a delay when the first time you use them. Preloading is a technology that downloads images to cache before they are needed. In this way, it is possible to quickly restore the image from the cache and display it immediately when it is really necessary.
Image() object
The easiest way to preload images is to create a new Image() object using JavaScript and then pass the URL of the image you want to preload to this object. Suppose we have an image file called, which we want to display when the user's mouse pointer moves to an existing image. In order to preload this file faster, we simply created a new Image() object called heavyImage and then loaded it on the page synchronously through the onLoad() event handle.
<html>
<head>
<script language = "JavaScript">
function preloader()
{
heavyImage = new Image();
= "";
}
</script>
</head>
<body onLoad="javascript:preloader()">
<a href="#" onMouseOver="javascript:document.=''">
<img name="img01" src=""></a>
</body>
</html>
Note that the tag of the image itself does not handle onMouseOver() and onMouseOut() events, which is why the <img> tag in the above example is included in the <a> tag. The tag <a> includes support for these event types.
Load multiple images through arrays
In actual situations, you are likely to need to preload more than one picture; for example, for menu bars that include multiple pictures, or want to achieve smooth animation effects. To achieve these is not difficult, you just need to use an array of JavaScript, as shown in the following example:
<script language="JavaScript">
function preloader()
{
// counter
var i = 0;
// create object
imageObj = new Image();
// set image list
images = new Array();
images[0]=""
images[1]=""
images[2]=""
images[3]=""
// start preloading
for(i=0; i<=3; i++)
{
=images[i];
}
}
</script>
In the example above, the variable i and the Image() object named imageObj are defined. Then a new array images[] is defined, and each array element will store the address source that needs to be preloaded. Finally, use a for() loop to iterate through the entire array and specify an Image() object for each element to preload the image into the cache.
Some browsers try to solve this problem by saving these images in a local cache. This allows these images to be called in sequence - but there is still a delay when the first time you use them. Preloading is a technology that downloads images to cache before they are needed. In this way, it is possible to quickly restore the image from the cache and display it immediately when it is really necessary.
Image() object
The easiest way to preload images is to create a new Image() object using JavaScript and then pass the URL of the image you want to preload to this object. Suppose we have an image file called, which we want to display when the user's mouse pointer moves to an existing image. In order to preload this file faster, we simply created a new Image() object called heavyImage and then loaded it on the page synchronously through the onLoad() event handle.
Copy the codeThe code is as follows:
<html>
<head>
<script language = "JavaScript">
function preloader()
{
heavyImage = new Image();
= "";
}
</script>
</head>
<body onLoad="javascript:preloader()">
<a href="#" onMouseOver="javascript:document.=''">
<img name="img01" src=""></a>
</body>
</html>
Note that the tag of the image itself does not handle onMouseOver() and onMouseOut() events, which is why the <img> tag in the above example is included in the <a> tag. The tag <a> includes support for these event types.
Load multiple images through arrays
In actual situations, you are likely to need to preload more than one picture; for example, for menu bars that include multiple pictures, or want to achieve smooth animation effects. To achieve these is not difficult, you just need to use an array of JavaScript, as shown in the following example:
Copy the codeThe code is as follows:
<script language="JavaScript">
function preloader()
{
// counter
var i = 0;
// create object
imageObj = new Image();
// set image list
images = new Array();
images[0]=""
images[1]=""
images[2]=""
images[3]=""
// start preloading
for(i=0; i<=3; i++)
{
=images[i];
}
}
</script>
In the example above, the variable i and the Image() object named imageObj are defined. Then a new array images[] is defined, and each array element will store the address source that needs to be preloaded. Finally, use a for() loop to iterate through the entire array and specify an Image() object for each element to preload the image into the cache.