JS control the size of the picture display (picture, etc. scale)

Source: Internet
Author: User

Http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/06/01/1749571.html

Code highlighting produced by Actipro Codehighlighter (freeware) http://www.codehighlighter.com/--><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> latest JavaScript automatically proportionally displays pictures, proportionally compresses picture display </title>
<script type= "Text/javascript" >
function Autoresizeimage (maxwidth,maxheight,objimg) {
var img = new Image ();
IMG.SRC = OBJIMG.SRC;
var hratio;
var wratio;
var Ratio = 1;
var w = img.width;
var h = img.height;
Wratio = maxwidth/w;
Hratio = maxheight/h;
if (maxWidth ==0 && maxheight==0) {
Ratio = 1;
}else if (maxwidth==0) {//
if (hratio<1) Ratio = Hratio;
}else if (maxheight==0) {
if (wratio<1) Ratio = Wratio;
}else if (wratio<1 | | hratio<1) {
Ratio = (wratio<=hratio?wratio:hratio);
}
if (ratio<1) {
w = w * Ratio;
H = h * Ratio;
}
Objimg.height = h;
Objimg.width = W;
}
</script>
<body>
<br/>
Original display (534 X) <br/>
Onload= "Autoresizeimage (0,0,this) <br/>
<a href= "Http://p.blog.csdn.net/images/p_blog_csdn_net/feng_sundy/421506/o_ 09925c7bcead8c60f683298dacf497a6.jpg "target=" _blank "></a><br/><br/>
1. Press width 250 compression, do not limit height proportionally to compress <br/>
Onload= "Autoresizeimage (250,0,this)" <br/>
<a href= "http://blog.csdn.net/feng_sundy/archive/2008/06/19/2566028.aspx" target= "_blank" ></a><br/><br/>
2. Press the height 250 compression, do not limit the width proportional compression <br/>
Onload= "Autoresizeimage (0,250,this)" <br/>
<a href= "http://blog.csdn.net/feng_sundy/archive/2008/06/19/2566028.aspx" target= "_blank" ></a><br/><br/>
3. Press the height 250 width 250 proportionally to compress the <br/>
Onload= "Autoresizeimage (250,250,this)" <br/>
<a href= "http://blog.csdn.net/feng_sundy/archive/2008/06/19/2566028.aspx" target= "_blank" ></a><br/><br/>
4. Aspect unequal proportional compression (X 512), when the height is constant, will automatically be compressed according to the height of the scale. <br/>
Onload= "Autoresizeimage (400,512,this)" <br/>
<a href= "http://blog.csdn.net/feng_sundy/archive/2008/06/19/2566028.aspx" target= "_blank" ></a><br/><br/>
5. Aspect unequal proportional compression (X 600), when the width is constant, will be automatically compressed by the proportion of the width. <br/>
Onload= "Autoresizeimage (300,600,this)" <br/>
<a href= "http://blog.csdn.net/feng_sundy/archive/2008/06/19/2566028.aspx" target= "_blank" ></a><br/><br/>
6. If the height and width of the picture are less than the maximum height and width of the compression, the picture will not be pulled up (shown by the original) <br/>
Original 444 x 207, compressed to x 600, will keep the original display <br/>
Onload= "Autoresizeimage (500,600,this)" <br/>
<a href= "http://blog.csdn.net/feng_sundy/archive/2008/06/19/2566028.aspx" target= "_blank" ></a><br/>< br/>
</body>

JS control the size of the picture display (picture, etc. scale)

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.