jquery implementation Proportional Scaling zoom image allows large picture Adaptive page layout _jquery

Source: Internet
Author: User
In the layout page, sometimes you will encounter large pictures of the page container "burst" situation, especially the loading of external chain pictures (usually through the collection of pictures of the outside station). So this article is going to tell you how to use jquery to zoom in on a picture to fit a large picture to the page layout.

Usually we work with thumbnails using background code (PHP,. NET, Java, etc.) based on large pictures to generate a certain size of the thumbnail, for the front page calls, of course, there is the use of the foreground JavaScript script will be loaded after the large image force scaling, into the so-called thumbnail, this method is not desirable. However, for site content pages, such as the website article details page, if you need to load a large picture, in order to prevent the "burst" layout, we use jquery to scale the picture proportionally. We will tell you two things:

1. Known picture size
Copy Code code as follows:

<div id= "Demo1" >

</div>

When a page loads a picture that contains the attribute width and height values, you can scale it using a few simple jquery code implementations.
Copy Code code as follows:

$ (function () {
var w = $ ("#demo1"). width ();//container widths
$ ("#demo1 img"). each (function () {//If there are many pictures, we can use each () to traverse
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
}
});
});

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.
Copy Code code as follows:

<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.

Autoimg can quickly adapt to the size of the article image, it uses the browser to get the image file head size data, without waiting for the picture to load complete.

autoimg Compatibility: Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 | ...

Calling the Autoimg plug-in method is fairly straightforward:
Copy Code code as follows:

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

autoimg instance Download

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.