jquery enables picture preload to increase page loading speed

Source: Internet
Author: User
Tags browser cache
Using jquery to implement picture preloading to improve page loading speed and user body we often encounter such problems when we do Web sites: A page with a large number of images causes the page to load slowly, often appear a white page user experience is not good. So how do we solve this problem? The first thing we will think of is to improve server performance, use static caching and other means to speed up the loading of pictures, this is a good way, but sometimes we can find some solutions from the front desk. Let me introduce a method of JS preloading which is often used in practical application. First in the output picture when we do some processing finished HTML we began to write JS, where I used the jquery class library $ (' img[data] '). Load (function () {var __this__ = $ (this); var url = __ This__.attr (' data '); var src = __this__.attr (' src '); if (url = = "| | url = = SRC)//Here to determine if the actual address of the picture does not exist or has been loaded does not process {return;} var img =newimage ();//Instantiate a picture of an object img.src = url;//The picture to be displayed is loaded Enter if (img.complete)//If the picture is already loaded in the browser cache directly processing {__this__.attr (' src ', url);//The picture to be displayed is replaced by return;} img.onload =function () {/ /To display the picture after loading done processing __this__.attr (' src ', url); }}); All right, try it quickly. is the page loading speed?

jquery enables picture preload to increase page loading speed

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.