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

Source: Internet
Author: User

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: Copy code

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, $ ("") here is to create a temporary img tag, similar to creating a new Image () object in js!
$ (""). 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, $ (" ")
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: Copy code
Window. onload = function (){
Function getViewSize () {// Get the width and height of the browser's viewpoint
Return {
"W": window ['initwidth'] | document.doc umentElement. clientWidth,
"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!

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.