Home >Web Front-end >JS Tutorial >Network image lazy loading implementation code beyond jquery control_javascript skills
Taobao, QQ, Dangdang, Amazon and other websites all have this effect. The principle is to load images in sections, which is mainly used for large websites to save bandwidth. However, the things they provide are compressed and confusing, and some of them require Based on some Yahoo library, it is very troublesome in short. JQ's control is clear enough, but it still does not achieve the effect of saving network bandwidth, so I made some improvements on the JQ lazy loading control. The biggest feature after the improvement is It is easy to call, does not require many changes in the project, and the JS file is not large. (Worker of Paopao.com! Please indicate the source for reprinting, thank you)
1. To develop controls for Paobu.com, I originally wanted to use JQ’s delay control directly, but JQ’s delay control only speeds up the loading speed and does not save bandwidth. That is to say, JQ’s lazy loading does not actually increase the image transmission volume. I don’t understand. I would like to ask you to DOWN the code and then accept it
{http://www.appelsiini.net/projects/lazyload This is the original JQ Download address of the control}
2. Pay attention to the usage of this control.
1. Quote JQ:
tag. Copy the code . The code is as follows :
Or call it like this
Of course you have to First there is a blue-loading.gif picture, and then it must be called under the images folder.
Set the display effect after the effect. The default is the SHOW effect.
4. Do not put it in the IMG tag at the front desk SRC, please put the original tag. For example,
This way, the image will not be loaded when the page is opened. This is also the most critical point, or the only thing that needs to be changed in the background code of the project.
There is code and there is truth
Code
It seems difficult for people to understand where I have optimized it.
First write down clearly the JQ lazy loading principle:
Suppose there are 10,000 images to be displayed on the page. JQ first transfers 10,000 images to client, and then the page quickly loads 10 pictures,
Save the loading time of 9990 pictures
My control principle:
First send 10 pictures to the client at one time, load 10 pictures,
It saves the loading time of 9990 pictures,
and the transmission time of 9990 pictures. The network traffic saved at this time is huge.
For large websites, these saved bandwidth can be used for things It’s gone
In other words, my control only loaded 1W IMG tags with empty values for the first time, which is just a little string!
(It is impossible to download the server image to the client without processing the original attribute in the IMG tag!
But if you use the SRC tag image, it will definitely be loaded the first time. At this time, We can only work on the page loading speed)
If you don’t believe it, you can use Firefox to view the image request. The JQ control will definitely complete the transmission in one go! )
By the way, let me tell you about my method of testing JQ
http://www.appelsiini.net/projects/lazyload/enabled.html This is the test address of the JQ control. Open it with Firefox, open firebug, and then monitor The amount of image transfer, the situation is that all 6 images are loaded as soon as they are opened, and when the scroll bar is dragged again, the loading function is simply executed, as shown in the picture above:
Source http://www.cnblogs.com/jacd/archive/2010/03/25/1696085.html