JavaScript determines whether a picture is loaded and finished

Source: Internet
Author: User
Tags jquery library

Sometimes we in the front-end development work in order to obtain the information of the picture, we need to be loaded after the picture is completed before the image can be correctly obtained size, and the corresponding callback function to make the picture to produce some kind of display effect. This paper mainly deals with several common methods of javascipt judgment picture loading, and explains it by combining code with practical application.

OnLoad method

Execute subsequent javascipt code by adding the OnLoad property to the IMG tag and filling in the appropriate function. The following code example shows that the IMG element is not displayed by default, and the image is displayed after the load is completed by onload.

<script type= "Text/javascript" > function Get (ts) {ts.style.display = ' block ';  Show Picture}</script>

Pros: You can load the JavaScript code part on any part of the page and can be used on most arbitrary images. The use is relatively simple and easy to understand.

Cons: The onlaod attribute must be affixed on each label, not applicable in some cases where the HTML code cannot be manipulated directly or requires code refinement

Javascipt Native method

Select the picture with the specified ID, specify the callback method via OnLoad, and the "picture loading completed" prompt pops up after the picture is loaded.

<script language= "JavaScript" >    document.getElementById ("Pic1"). onload = function () {        alert ("Picture loading Completed");    } </script>

Pros: Easy to use, without affecting HTML code.

Disadvantage: Only one element can be specified, Javascipt code must be placed below the picture element

jquery method

Bind events for each picture element of class Pic1, and use the jquery load method to get the elements out of the way.

Note that you do not bind the Load event in the. Ready ().

<script type= "Text/javascript" >$ (function () {$ ('. Pic1 '). each (function () {$ (the). Load (function () {$ (this). FadeIn ();});})    </script>

Pros: You can bulk bind element events without affecting HTML code content

Cons: Requires the support of the jquery library, and the code needs to be placed below the element that needs to be manipulated.

JavaScript determines whether a picture is loaded and finished

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.