Update: 05.27:1. Ensure callback execution order: error > Ready > Load;2, callback function This points to the IMG itself 04-02:1, increase the picture after full load callback 2, improve performance /** * Picture header Data Load Ready event-get picture size faster * @version 2011.05.27 * @author Tangbin * @param {String} picture path * @param {Function} dimensions ready * @param {Function} loaded complete (optional) * @param {Function} load error (optional) * @example imgready (' Yun_qi_img/logo_cn.png ', function () { Alert (' Size ready:width= ' + this.width + '; height= ' + this.height); }); */ var Imgready = (function () { var list = [], intervalid = null, Used to execute queues 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 picture is cached, the cached data is returned directly if (img.complete) { Ready.call (IMG); Load && load.call (IMG); Return }; width = img.width; Height = img.height; Events after loading errors Img.onerror = function () { Error && Error.call (IMG); Onready.end = true; img = img.onload = Img.onerror = null; }; Picture size Ready Onready = function () { Newwidth = Img.width; Newheight = Img.height; if (newwidth!== width | | | newheight!== Height | | If the picture has been loaded elsewhere, the usable area can be detected Newwidth * newheight > 1024 ) { Ready.call (IMG); Onready.end = true; }; }; Onready (); Fully Loaded Events Img.onload = function () { OnLoad may be faster than Onready in the timer time difference range Check here and ensure Onready priority !onready.end && Onready (); Load && load.call (IMG); IE gif animation will loop to execute onload, empty onload can be img = img.onload = Img.onerror = null; }; To join a queue for periodic execution if (!onready.end) { List.push (Onready); Reduces browser performance loss whenever only one timer is allowed if (intervalid = = null) Intervalid = SetInterval (tick, 40); }; }; })(); |