JavaScript picture pre-loaded to make picture pixels clearer

Source: Internet
Author: User

3. When the client requests the map, 4M of the original image will not immediately under the good, especially the remote network, then first put the small map down, and pulled up to the original size (this operation should be the use of no interpolation algorithm direct amplification, speed is very fast.) )


Should be stored in a number of different sizes and different resolution of many pictures, when the big picture did not load the first successive display of small pictures, and finally show the big picture

The code is as follows Copy Code

var img = new Image ();
IMG.SRC = "Yun_qi_img/big_800_600.jpg";
Img.onload = function () {
document.getElementById (' myimage '). src = this.src;
}


JavaScript image Pre-loading technology

Lightbox class effects use preload to center the picture and need to wait for the full load to be displayed and experience poorly (such as the full screen effect of the Filick album). JavaScript cannot get IMG header data, is that really the case? This article is an ingenious way to get JavaScript to get it.

This is an example of how most people use preload to get the size of a picture:


Of course there will be some compatible traps, such as width and height to detect the inconsistencies of each browser, as well as WebKit new image () created by the image in the loading process with the URL image, after repeated testing the best way to deal with:

The code is as follows Copy Code

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);

};

};

})();

Call Example:

The code is as follows Copy Code

Imgready (' Yun_qi_img/logo_cn.png ', function () {

Alert (' Size ready:width= ' + this.width + '; height= ' + this.height);

});

Related Article

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.