How does js and jquery get the true width and height of an image? How does jsjquery get the width?

Source: Internet
Author: User

How does js and jquery get the true width and height of an image? How does jsjquery get the width?

1. When do I need to obtain the true width and height of the image?

When creating a pc webpage, you may sometimes consider judging whether the image is a horizontal or vertical image based on the size of the inserted image. Then, different display methods will be given after judgment!

The other is that on the mobile phone page, the picture inserted on the news page is usually displayed according to the original size of the picture. If the screen on the mobile phone is too small, the picture that is too big will go beyond! There are two solutions at this time

1) Add this style to all the images.

1
Copy codeThe Code is as follows:
. News img {margin: 5px auto; display: block; width: 100%; height: auto ;}

However, another problem with this method is that if the inserted image itself is small, it will be stretched to 100% for display. Obviously this is unreasonable! Another method is to dynamically display the image size through js!

2) js dynamically obtains the image size

Jquery

The Code is as follows:

Var _ w = parseInt ($ (window ). width (); // get the browser width $ (". new_mess_c img "). each (function (I) {var img = $ (this); var realWidth; // The actual width var realHeight; // The actual height // here is explained, $ (" "). attr ("src", $ (img ). attr ("src ")). load (function () {/* if you want to obtain the true width and height of an image, you must note that 1. Create an image object: for example, $ (" = _ w){((img..css ("width", "100%" ).css ("height ", "auto");} else {// If the width of the browser is smaller than the original size, the following figure is displayed: (imgw..css ("width", realwidth+'px'}.css ("height", realHeight + 'px ');}});});

Js Mode

The Code is as follows:

Window. onload = function () {function getViewSize () {// returns {"w": window ['innerwidth'] | document.doc umentElement. clientWidth, (www.jb51.net) "h": window ['innerheight'] | document.doc umentElement. clientHeight} function getFullSize () {// obtain the maximum width of the browser var w = Math.max(document.doc umentElement. clientWidth, document. body. clientWidth) implements math.max(document.doc umentElement. scrollLeft, document. body. scrollLeft); var h = Math.max(document.doc umentElement. clientHeight, document. body. clientHeight) implements math.max(document.doc umentElement. scrollTop, document. body. scrollTop); w = Math.max(document.doc umentElement. scrollWidth, w); h = Math.max(document.doc umentElement. scrollHeight, h); return {"w": w, "h": h };}var _ sv_w = getViewSize () ["w"]; var _ sf_w = getFullSize () ["w"]; var _ w = _ sv_w; // The width of the view is used here. For details, see var Imgarray = document. getElementsByTagName ("img"); var realWidth; // The true width var realHeight; // The true height for (var I = 0; I  = _ w) {Imgarray [_ stemp. index]. style. width = _ w + 'px '; Imgarray [_ stemp. index]. style. height = 'auto';} else {Imgarray [_ stemp. index]. style. width = realWidth + 'px '; Imgarray [_ stemp. index]. style. height = realHeight + 'px ';}}}}

In the above two methods, jquery is relatively simple and quick to implement. The second method is more complicated, but it is faster to execute than jquery!


How to obtain the image height and width in js

<Script>
Var image = new Image ();
Image. src = 'www .baidu.com/img/logo.gif ';
Image. onreadystatechange = function (){
If (image. readyState = "complete "){
Alert (["image size:", image. width, image. height]);
}
}
</Script>
Try again...
The image is not fully loaded.

I want to use js to obtain the width and height of an image. How can I obtain the image in the label?



<Script type = "text/javascript">
Function getWH (){
Var id = "_ img ";
Var w = 0;
Var h = 0;
// Good versatility
Var real = document. getElementById (id). height | document. getElementById (id). style. height | document. getElementById (id). offsetHeight;
H = parseInt (real );
Real = document. getElementById (id). width | document. getElementById (id). style. width | document. getElementById (id). offsetWidth;
W = parseInt (real );
Alert ("w:" + w + "; h:" + h)
}
GetWH ();

</Script>

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.