Js and jquery get the true width and height of the image, and jsjquery get the width

Source: Internet
Author: User

Js and jquery get the true width and height of the image, and 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
. 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 (); // obtain 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 the description, $ ("$ ("/*
If you want to obtain the true width and height of an image, you must note that
1. Create an image object. For example, $ (" ")
2. Specify the src path of the image.
3. After the image is loaded, execute the function such as. load ().
*/
RealWidth = this. width;
RealHeight = this. height;
// If the actual width is greater than the browser width, the display will be 100%
If (realWidth> = _ w ){
((Img).css ("width", "100%" ).css ("height", "auto ");
}
Else {// If the browser width is smaller than the original size
((Img).css ("width", realwidth+'px'0000.css ("height", realHeight + 'pxy ');
}
});
});

Js Mode

 

The Code is as follows:
Window. onload = function (){
Function getViewSize () {// get the width and height of the browser's Viewpoint
Return {
"W": window ['initwidth'] | document.doc umentElement. clientWidth,
(Www.111cn.net) "h": window ['innerheight'] | document.doc umentElement. clientHeight
}
}
Function getFullSize () {// obtain the maximum browser width
Var w = Math.max(document.doc umentElement. clientWidth, document. body. clientWidth) +
Math.max(document.doc umentElement. scrollLeft, document. body. scrollLeft );
Var h = Math.max(document.doc umentElement. clientHeight, document. body. clientHeight) +
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, depending on the situation.
Var Imgarray = document. getElementsByTagName ("img ");
Var realWidth; // The actual width.
Var realHeight; // The actual height.
For (var I = 0; I Var imgtemp = new Image (); // create an image object
Imgtemp. src = Imgarray [I]. src;
Imgtemp. index = I; // specify a search value to determine the image.
Imgtemp. onload = function () {// After the image is loaded, run
Var _ stemp = this; // copy the current pointer to a new variable. Otherwise, the variable will be shared.
RealWidth = this. width;
RealHeight = this. height;
If (realWidth> = _ 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!
From: http://www.111cn.net/wy/jquery/66979.htm


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.

Want to create a JS to get the width and height of the image?


<Script>
Var img = document. getElementById ("t ")
Var temp = new Image ();
Temp. onload = function (){
Var style = img. style,
Ratio = Math. min (1,
Math. max (0,100)/this. width | 1,
Math. max (0,100)/this. height | 1
);
// Set preview size
Style. width = Math. round (this. width * ratio) + "px ";
Style. height = Math. round (this. height * ratio) + "px ";
}
Temp. src = img. src;
</Script>

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.