JavaScript Picture Loading---a solution for loading large graphs

Source: Internet
Author: User

Web page loading a large picture, often to load a long time;

Moreover, in the loading process, can not well control the style of the picture, prone to dislocation and other display errors;

If you can load a large image, first use a smaller loading picture placeholder, and then load the large picture in the background, when the large picture is loaded, automatically replace the bitmap,

This will provide a better user experience;

Because, I was developing a picture viewer, encountered such a demand, so I wrote a angular service to solve this problem, the effect is good;

Although it is a angular service, it can be used alone if the core code is extracted from it;

To share the following source code:

One:

    var imgloader = new Object ();        Imgloader.placeholder = new Image ();

Imgloader is the main object

Placeholder for storing placeholder images

Two:

        Imgloader.init = function (placeholderpath,width,height) {            imgloader.placeholder.src = Placeholderpath;            Imgloader.placeholder.width = width;            imgloader.placeholder.height = height;        }

Init is Imgloader initialization method, mainly specifies the placeholder picture file, has its width high;

The purpose of setting the width is to be able to layout when the occupied bitmap is not yet loaded

Three:

Imgloader.load = function (imgelem,imgsrc,callback) {//Clears the previous onload function if (imgelem.lastload) {Imgelem        . lastload.onload = function () {};        } Loadok = false;            var testimg = new Image ();        TESTIMG.SRC = IMGSRC;           if (Testimg.complete = = true) {imgelem.src = TESTIMG.SRC;           Imgelem.width = Testimg.naturalwidth;           if (Imgelem.hasattribute ("height")) {Imgelem.removeattribute ("height");           }}else{imgelem.src = IMGLOADER.PLACEHOLDER.SRC;           Imgelem.width = Imgloader.placeholder.width;           Imgelem.height = Imgloader.placeholder.height;               Read-only properties//Imgelem.naturalwidth = imgelem.width;//Imgelem.naturalheight = imgelem.height;// Console.log (imgelem.naturalwidth+ "|"           +imgelem.naturalheight);               Bind onload Function testimg.onload = function () {imgelem.src = TESTIMG.SRC; Imgelem.width = tesTimg.naturalwidth;               if (Imgelem.hasattribute ("height")) {Imgelem.removeattribute ("height");                } if (callback) {callback ();                       }           };        Imgelem.lastload = testimg;//Imgloader.loadingArray.push (imgelem); }    };


1. At first, if more than one load is set on the same picture element, only the last one will be replaced;

2. Use the src of the large image to set up an IMG to do the test, if this picture has been loaded, then directly set up this large map;

3. Note here that I have restored the width height of the element being set, in order to avoid being affected by the width height of the set element received by the placeholder;

4. Tip: When the width is set, the Height property is removed, and the height is adjusted to the width

5. If the testimg (larger view) is not loaded, then replace with the occupied bitmap, note that the width of the set picture is modified to the same size as the bitmap;

6. To bind the OnLoad function for testimg (larger view), when the large graph is loaded, the src of the set element is replaced by the src of the larger image, and the box height of the large image is restored;

7. There are set callback, you can do something in the callback;

8. Set this imgelem has been bound to a large map (again bound, this will be eliminated)

9. Tips: Naturalwidth and naturalheight are read-only properties

Four:

An example:

As can be seen in the example: CSS can also be used to control the size of the image set

Five:

Full code:

Miku Picture loader 3.0var imgloader = new Object ();    Imgloader.placeholder = new Image ();//Imgloader.loadingarray = new Array ();        Imgloader.init = function (placeholderpath,width,height) {imgloader.placeholder.src = Placeholderpath;        Imgloader.placeholder.width = width;    Imgloader.placeholder.height = height;    }; Imgloader.load = function (imgelem,imgsrc,callback) {//Clears the previous onload function if (imgelem.lastload) {Imgelem        . lastload.onload = function () {};            } var testimg = new Image ();        TESTIMG.SRC = IMGSRC;           if (Testimg.complete = = true) {imgelem.src = TESTIMG.SRC;           Imgelem.width = Testimg.naturalwidth;           if (Imgelem.hasattribute ("height")) {Imgelem.removeattribute ("height");           }}else{imgelem.src = IMGLOADER.PLACEHOLDER.SRC;           Imgelem.width = Imgloader.placeholder.width;           Imgelem.height = Imgloader.placeholder.height;       Read-only Properties//        Imgelem.naturalwidth = imgelem.width;//Imgelem.naturalheight = imgelem.height;//cons Ole.log (imgelem.naturalwidth+ "|"           +imgelem.naturalheight);               Bind onload Function testimg.onload = function () {imgelem.src = TESTIMG.SRC;               Imgelem.width = Testimg.naturalwidth;               if (Imgelem.hasattribute ("height")) {Imgelem.removeattribute ("height");                } if (callback) {callback ();                       }           };        Imgelem.lastload = testimg;//Imgloader.loadingArray.push (imgelem); }};////Clears all onload functions of the load queue//function Clearonload (Loadingarray) {//for (Var i=0;i<loadingarray.length;i+ +) {//var loading = loadingarray[i];//Loading.onload = function () {};//}//};



JavaScript Picture Loading---a solution for loading large graphs

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.