Real-time loading of js images provides webpage opening speed _ javascript skills

Source: Internet
Author: User
There is no need to load all the images at the beginning, so that the speed of opening the network is greatly improved. Here is a good idea. You can look at the large websites, in particular, when there are many pictures, such as large e-commerce websites, you will find that the pictures are loaded only when you scroll down the first screen, there is no need to load all the images at the beginning, which improves the speed of opening the network. The following are the ideas that I have come up with. If you have a better idea, I hope you can give me some advice.

In fact, the idea of loading images in real time is very simple. When loading a page at the beginning, you can see images that do not need to be loaded at the very beginning (for example, images below the second screen must be rolled down, it is better to set them to scroll down and load them in real time) path to a custom property, such as:, src = "http://ctc.qzonestyle.gtimg.cn/ac/ B .gif" is a 1px * 1px transparent GIF image, the "http://y3.ifengimg.com/a/2014_37/03c4830691a6d8b.jpg" in the custom property data-src is the path of the image. When you scroll down to it, replace the value in data-src with the value of src, then, remove the data-src attribute ......

The following is a demo.

 Real-time loading of js Images

Http://www.cnblogs.com/xiaomou2014cont2

Cont1

Cont2

Cont3

Script // compatible with IE for getElementsByClassNameif (navigator. appName = 'Microsoft Internet Explorer ') {document. getElementsByClassName = function () {var tTagName = "*"; if (arguments. length> 1) {tTagName = arguments [1];} if (arguments. length> 2) {var pObj = arguments [2]} else {var pObj = document;} var objArr = pObj. getElementsByTagName (tTagName); var tRObj = new Array (); for (var I = 0; I <objArr. length; I ++) {if (objArr [I]. className = arguments [0]) {tRObj. push (objArr [I]) ;}} return tRObj ;}// image loading function loadImg (imgClass, imgSrc) {/data-src: path for storing the image to be loaded var arrImg = document. getElementsByClassName (imgClass); // The image Array window whose class is imgClass needs to be loaded in real time. onscroll = function () {for (var I = 0; iarrImg [I]. offsetTop) {arrImg [I]. setAttribute ("src", arrImg [I]. getAttribute (imgSrc); arrImg [I]. removeAttribute (imgSrc) ;}}} loadImg ("aimg", "data-src"); script

The images in cont1 in the demo are displayed as soon as the page is loaded, and the homepage layout is fixed. You can confirm that the images are under the second screen, but for the content page, the position of the image in the subject content container is uncontrollable. This requires you to determine whether the image is within the visible range after loading the real page, if it is loaded within the visible range (of course, the home page can do the same, but if the network speed is slow, when you open the home page, you need to wait until the entire page architecture is loaded to display the image, instead of loading it from top to bottom.) You need to set the width and height attributes for the image, this allows it to take a place on the page, otherwise it will only take the size of the transparent GIF image before loading.

The following demo makes some modifications to the content page. loading is triggered during window. onload and window. onscroll.

 Real-time loading of js Images

Http://www.cnblogs.com/xiaomou2014

19-year-old girl with advanced cancer who had been cut off 7 years ago

Http://www.cnblogs.com/xiaomou2014

Http://www.cnblogs.com/xiaomou2014

Http://www.cnblogs.com/xiaomou2014

19-year-old girl with advanced cancer who had been cut off 7 years ago

19-year-old girl with advanced cancer who had been cut off 7 years ago

19-year-old girl with advanced cancer who had been cut off 7 years ago

19-year-old girl with advanced cancer who had been cut off 7 years ago

19-year-old girl with advanced cancer who had been cut off 7 years ago

Script // compatible with IE for getElementsByClassNameif (navigator. appName = 'Microsoft Internet Explorer ') {document. getElementsByClassName = function () {var tTagName = "*"; if (arguments. length> 1) {tTagName = arguments [1];} if (arguments. length> 2) {var pObj = arguments [2]} else {var pObj = document;} var objArr = pObj. getElementsByTagName (tTagName); var tRObj = new Array (); for (var I = 0; I <objArr. length; I ++) {if (objArr [I]. className = arguments [0]) {tRObj. push (objArr [I]) ;}} return tRObj ;}// image loading function loadImg (imgClass, imgSrc) {/data-src: path for storing the image to be loaded var arrImg = document. getElementsByClassName (imgClass); // The Image array whose class is imgClass needs to be loaded in real time for (var I = 0; iarrImg [I]. offsetTop) {arrImg [I]. setAttribute ("src", arrImg [I]. getAttribute (imgSrc); arrImg [I]. removeAttribute (imgSrc) ;}} window. onload = window. onscroll = function () {loadImg ("aimg", "data-src")} script

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.