jquery implementation proportional Scaling picture size example

Source: Internet
Author: User

1. Know picture height and width

When the page loads the picture contains attribute width and height value, you can use a few simple jquery code to achieve proportional scaling, or through the JQ built-in function to get the picture width and height.

The code is as follows Copy Code

$ (function () {

var w = $ ("#demo1"). width ();//container widths
$ ("#demo1 img"). each (function () {//If there are many pictures, you can jq the built-in each () traversal
var img_w = $ (this). width ();//Picture widths

var Img_h = $ (this). Height ();//Picture Heights

if (img_w>w) {//If the picture is wider than the width of the container-it's going to burst.

var height = (w*img_h)/img_w; Height ratio Scaling

$ (this). css ({"width": w, Height: height})//set width and height after scaling
}

});

});

Hey, it's simpler than JS! Minute to finish the article content page multiple map size caused "out of line" problem!

2. Unknown picture size

When the page load picture size is not known, the above code can not be effectively scaled, this situation more than the current collection of external linked pictures.

The code is as follows Copy Code

<div id= "Demo2" >

</div>

Fortunately, a good friend has written a special plug-in to deal with, and across the browser, to solve the front-end friends of a big problem.

Below is a grand introduction to the next autoimg.

Calling the Autoimg plug-in method is fairly straightforward:

  code is as follows copy code

$ (function ( {
$ ("#demo2"). Autoimg ();
});

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.