Code and call Method for proportional scaling of webpage images using JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
To ensure the uniform size of the image, setting the image size directly causes the image to stretch and blur the image. Then, we will introduce the code to automatically adjust the image size proportionally after the image is loaded, if you are interested, you can refer to the following: when processing web images, especially in some image list applications, it is difficult to ensure the uniform size of images. Setting the image size directly will lead to image stretching, the code described in this article can automatically adjust the image size proportionally after the image is loaded.
Javascript:

The Code is as follows:


<Script language = "javascript" type = "text/javascript">
<! --
// Description: proportional scaling of webpage images using JavaScript
// Finishing: http://www.CodeBit.cn
Function DrawImage (ImgD, FitWidth, FitHeight)
{
Var image = new Image ();
Image. src = ImgD. src;
If (image. width> 0 & image. height> 0)
{
If (image. width/image. height> = FitWidth/FitHeight)
{
If (image. width> FitWidth)
{
ImgD. width = FitWidth;
ImgD. height = (image. height * FitWidth)/image. width;
}
Else
{
ImgD. width = image. width;
ImgD. height = image. height;
}
}
Else
{
If (image. height> FitHeight)
{
ImgD. height = FitHeight;
ImgD. width = (image. width * FitHeight)/image. height;
}
Else
{
ImgD. width = image. width;
ImgD. height = image. height;
}
}
}
}
// -->
<Script>


Call method:
Code:

The Code is as follows:




If the image size is large, we recommend that you set the expected image size in the Image Tag. This will not cause the page to be opened during loading, and this size will not affect the final scaling effect. You can modify the above Code:
Code:

The Code is as follows:


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.