Js determines how to obtain the actual width and height of an image after the image is loaded. js Load

Source: Internet
Author: User

Js determines how to obtain the actual width and height of an image after the image is loaded. js Load

This article describes how to obtain the actual width and height of an image after js judges that the image is loaded. We will share this with you for your reference. The details are as follows:

Normally, we use jq. width ()/. the height () method is used to obtain the width/height of an image or js. offsetwidth /. the offsetheight method is used to obtain the width and height of an image. However, after we set the width and height of an image through a style, the actual width and height of the image are not obtained, this is obviously not the expected result in some cases. Is there a way to obtain the actual width and height? The answer is yes. The following code can solve this problem:

 

Js code:

// Obtain the actual width and height of the image after the image is loaded. var _ test = document. getElementById ("test"); test. onload = function () {imgSize. call (_ test);} function imgSize () {var imgObj = new Image (); imgObj. src = this. src; alert (imgObj. width + "\ n" + imgObj. height );}

If you want to call this actual width and height in other methods, you should set alert (imgObj. width + "\ n" + imgObj. height); change to return imgObj, and then call the method:

window.onload = function(){    function a(){      var real= imgSize.call(_test);      var realwidth = real.width;      alert(realwidth);    }    a();}

The above method is too cumbersome and has been refined by myself. It is abbreviated as follows:

Window. onload = function () {var _ test = document. getElementById ("test"); // If jq is used, replace the Code with var _ test =$ ("# test. Var imgObj = new Image (); imgObj. src = _ test. src; // If jq is used, replace the Code with imgObj. src = _ test. attr ("src. Alert (imgObj. width );}

In this way, you can directly call the actual width and height of the image in other methods.

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.