How to get the true width and height of Images Using js and jquery _ javascript skills

Source: Internet
Author: User
Determine whether the image is a horizontal or vertical image based on the size of the inserted image. After judging the image, we will give you different display methods. The following describes how js and jquery obtain the true width and height of the image. 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

The 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, $ ("") here is to create a temporary img tag, similar to js to create a new Image () object! $ (""). 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, as shown in $ ("") 2. Specify the src path of the image. 3. After the image is loaded, run the following command. run */realWidth = this in the load () function. width; realHeight = this. height; // if the actual width is greater than the browser's width, display if (realWidth> = _ 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!

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.