Js uses image objects to pre-load images to improve access speed _ javascript skills

Source: Internet
Author: User
Let's learn a little trick called imagepreloading to speed up image access. Some browsers try to solve this problem by saving these images in the local cache, interested friends can understand that a large number of high-resolution images can indeed make a Web site shine. However, the Website access speed may also decrease. If the image is a file, the file will occupy the bandwidth, and the bandwidth is directly related to the access wait time. Now, let's learn a little trick called image preloading to speed up image access.

Some browsers try to solve this problem by saving these images in the local cache. In this way, you can call these images in sequence-however, there will still be latency when using these images for the first time. Pre-loading is a technology that downloads images to the cache before they are needed. This method can be used to quickly restore an image from the cache and display it immediately when the image needs to be displayed.

Image () object
The simplest method of Image pre-loading is to create a new Image () object using JavaScript, and then pass the Image URL you want to pre-load to this object. Assume that we have an image file named heavyimagefile.jpg. We hope this file will be displayed when you move your mouse pointer to an existing image. In order to facilitate the pre-loading of this file, we simply created a new Image () object named heavyImage, and then loaded it to the page synchronously through the onLoad () event handle.

The Code is as follows:




Related Article

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.