Javascript image pre-loading instance analysis _ javascript tips-js tutorial

Source: Internet
Author: User
Tags intl
This article mainly introduces the method of javascript image pre-loading. The example analyzes the method of javascript to implement image pre-loading and related precautions, which has some reference value, for more information about how to pre-load javascript images, see the following example. Share it with you for your reference. The details are as follows:

The lightbox effect is pre-loaded to enable the picture to be displayed in the center. You need to wait until it is fully loaded to display the effect. The experience is poor (such as the full screen effect of the filick album ). Javascript cannot obtain the imgfile header data. Is that true? This article uses a clever method for javascript to obtain it.

This is an example of how most people use the pre-loading method to obtain the image size:

var imgLoad = function (url, callback) {  var img = new Image();  img.src = url;  if (img.complete) {    callback(img.width, img.height);  } else {    img.onload = function () {      callback(img.width, img.height);      img.onload = null;    };  };};

JavaScript code:

// Update: // 05.27: 1. Ensure the callback execution sequence: error> ready> load; 2. the callback function this points to the img itself // 04-02: 1. added the callback after the image is fully loaded. 2. Improved performance./*** image header data loading readiness event-faster image size acquisition * @ version 2011.05.27 * @ author TangBin * @ see http://www.planeart.cn/?p=1121 * @ Param {String} image path * @ param {Function} Ready size * @ param {Function} loaded (optional) * @ param {Function} loading error (optional) * @ example imgReady (' http://www.google.com.hk/intl/zh-CN/images/logo_cn.png ', Function () {alert ('size ready: width =' + this. width + '; height =' + this. height) ;}); */var imgReady = (function () {var list = [], intervalId = null, // used to run the queue tick = function () {var I = 0; for (; I <list. length; I ++) {list [I]. end? List. splice (I --, 1): list [I] () ;};! List. length & stop () ;}, // stop all timer queues stop = function () {clearInterval (intervalId); intervalId = null ;}; return function (url, ready, load, error) {var onready, width, height, newWidth, newHeight, img = new Image (); img. src = url; // if the image is cached, the cached data if (img. complete) {ready. call (img); load & load. call (img); return ;}; width = img. width; height = img. height; // load the error event img. onerror = functi On () {error & error. call (img); onready. end = true; img = img. onload = img. onerror = null ;}; // image size ready onready = function () {newWidth = img. width; newHeight = img. height; if (newWidth! = Width | newHeight! = Height | // if the image has been loaded elsewhere, use area detection newWidth * newHeight> 1024) {ready. call (img); onready. end = true ;};}; onready (); // fully loaded event img. onload = function () {// onload may be faster than onready In the timer time difference range. // check here and ensure that onready is executed preferentially! Onready. end & onready (); load & load. call (img); // The ie gif animation executes onload cyclically and sets the onload to img = img. onload = img. onerror = null ;}; // periodically execute if (! Onready. end) {list. push (onready); // only one timer is allowed at a time, reducing browser performance loss if (intervalId = null) intervalId = setInterval (tick, 40 );};};})();

Call example:

imgReady('http://www.google.com.hk/intl/zh-CN/images/logo_cn.png', function () {  alert('size ready: width=' + this.width + '; height=' + this.height);});

I hope this article will help you design javascript programs.

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.