Principles and Applications of image delayed loading and image delayed Loading

Source: Internet
Author: User

Principles and Applications of image delayed loading and image delayed Loading

The basic theory of delayed image loading:
When we browse a page, we sometimes do not read all the content of a page, so when the customer does not browse all the content, loading all images will waste traffic and affect the loading time of webpages. To solve this problem, we designed a mechanism for loading images at a time delay, that is, when users slide pages to images, we start to load images, which saves traffic, and does not affect the user experience.

The following is an example of multiple delayed loading tasks:
First, create the HTML Tag and style required.

    <style type="text/css">        body, div, img {            margin: 0;            padding: 0;        }        img {            border: none;        }        #con {            margin: 10px auto;            padding: 15px 0 15px 15px;            width: 630px;            border: 1px dashed #ff3333;            border-radius: 10px;            overflow: hidden;        }        #con div {            float: left;            margin: 15px 15px 0 0;            width: 300px;            height: 150px;            background: url("img/ddd.jpg") no-repeat;        }        #con div img {            width: 300px;            height: 150px;            opacity: 0;        }    </style>

The overall idea is as follows: First we get the large container for loading images, and then create a div for loading each image so that it occupies a proper position, then, when the user slides to the top of the current div, the image is loaded. After the image is loaded, it is displayed. To prevent the image from being loaded for multiple times, we add an identifier after the image is loaded to prevent the image from being loaded for multiple times.
The code and comments are as follows:

Var date = []; for (var I = 0; I <100; I ++) {var ran = Math. round (Math. random () * 9 + 1); date [date. length] = "img/" + ran + ". jpg ";} // Create the position div; var conBox = document. getElementById ("con"); var frg = document. createDocumentFragment (); for (var I = 0; I <date. length; I ++) {var imgBox = document. createElement ("div"); imgBox. trueSrc = date [I]; frg. appendChild (imgBox);} conBox. appendChild (frg ); // Obtain the height function of the browser getWin (attr) {return document.doc umentElement [attr] | document. body [attr];} // function offset (ele) {var h = ele. offsetTop; var l = ele. offsetLeft; var p = ele. offsetParent; while (p) {if (navigator. userAgent. indexOf ("MSIE 8")>-1) {h + = p. offsetTop; l + = p. offsetLeft;} else {l + = p. offsetLeft + p. clientLeft; h + = p. offsetTop + p. clientTop} p = p. offsetPa Rent ;}return {top: h, left: l };}// function move (oImg) {var n = 0; var timer = window. setInterval (function () {oImg. style. opacity = n; if (n> 0.99) {oImg. style. opacity = 1; clearInterval (timer);} n + = 0.01 ;}, 10)} function loadImg (imgBox) {var oImg = new Image; oImg. src = imgBox. trueSrc; oImg. onload = function () {imgBox. appendChild (oImg); imgBox. loadOk = true; move (oImg );}} // Bind the window function start () {var cur = getWin ("scrollTop") + getWin ("clientHeight"); var imgBox = conBox. getElementsByTagName ("div"); for (var I = 0; I  offset (temp ). top) {if (! Temp. loadOk) {loadImg (temp) ;}}} window. onload = start; window. onscroll = start;

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.