Today we will introduce how to use jquery's jQuery. autoIMG. min plug-in to compare and scale images. I hope this tutorial will be helpful to you.
This section describes autoIMG.
AutoIMG can quickly adapt the size of the article image. It uses a browser to obtain the size data of the image file header, without waiting for the image to be loaded.
AutoIMG compatibility: Chrome | Firefox | Sifari | Opera | IE6 | IE7 | IE8 |...
Calling the autoIMG plug-in is quite simple:
| The Code is as follows: |
Copy code |
$ (Function (){ $ ("# Demo2"). autoIMG (); }); |
See the instance below
| The Code is as follows: |
Copy code |
| <Script type = "text/javascript" src = "../js/jquery. js"> </script> <Script type = "text/javascript" src = "jQuery. autoIMG. min. js"> </script> <Style type = "text/css"> . Demo {width: 600px; margin: 20px auto; overflow: hidden} </Style> <Script type = "text/javascript"> $ (Function (){ $ ("# Demo2"). autoIMG ();
Var w = $ ("# demo1"). width (); $ ("# Demo1 img"). each (function (){ Var img_w = $ (this). width (); Var img_h = $ (this). height (); If (img_w> w ){ Var height = (w * img_h)/img_w; ((This).css ({"width": w, "height": height }); } }); }); </Script> </Head> <Body> <Div class = "demo">
<P> 1. known image size <a href = "images/one.jpg" target = "_ blank"> View large image </a> </p> <Div id = "demo1">
</Div> <P> 2. Unknown image size <a href = "/jQueryautoImg/images/two.jpg" target = "_ blank"> View large image size </a> </p> <Div id = "demo2"> <br> </Div> </Div> </Div> |
Next we will analyze the instance
There are two cases:
1. known image size
| The Code is as follows: |
Copy code |
<Div id = "demo1">
</Div> |
When the page-loaded image contains the width and height values, you can use a few simple jQuery code statements to achieve proportional scaling.
| The Code is as follows: |
Copy code |
| $ (Function (){ Var w = $ ("# demo1"). width (); // container width $ ("# Demo1 img"). each (function () {// if there are many images, we can use each () to traverse Var img_w = $ (this). width (); // Image width Var img_h = $ (this). height (); // Image height If (img_w> w) {// if the image width exceeds the container width, the image must be broken. Var height = (w * img_h)/img_w; // proportional height Scaling ((This).css ({"width": w, "height": height}); // you can specify the width and height of the scaled image. } }); }); |
2. Unknown image size
When the size of the image to be loaded on the page is unknown, the code above cannot be effectively scaled. In this case, more external linked images are collected.
| The Code is as follows: |
Copy code |
| <Div id = "demo2">
</Div> |
Fortunately, some kind friends have already written specialized plug-ins to handle the issue, and cross-browser solutions solve a major problem for front-end friends.