JS makes the image adaptive in the image box and scales proportionally

Source: Internet
Author: User

<SCRIPT type = "text/JavaScript">

// Imgd: the IMG element of the image to be placed. This is available for parameter passing during onload.

// H: height of the IMG element, pixel

// W: IMG element width, pixel
Function autosize2 (imgd, H, W)
{
VaR image = new image ();
Image. src = imgd. SRC;
If (image. width <W & image. height {
Imgd. width = image. width;
Imgd. Height = image. height;
}
Else
{
If (W/H <= image. width/image. Height)
{
Imgd. width = W;
Imgd. Height = W * (image. Height/image. width );
}
Else
{
Imgd. width = H * (image. width/image. Height );
Imgd. Height = h;
}
}

// The picture is centered, IE8 is effective, ie9 is ineffective, Firefox is ineffective, please use table to center on the page
// Imgd. style. paddingleft = (W + 20-imgd. width)/2; // 20 indicates the sum of the distance between padding-left and padding-right.
// Imgd. style. paddingtop = (H + 20-imgd. Height)/2; // 20 indicates the sum of the distance between padding-top and padding-bottom.
}
</SCRIPT>

 

Call this function when loading the graph (IMG onload event)

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.