Home > Web Front-end > JS Tutorial > JavaScript implements equal-proportion scaling of web page images code and calling method_javascript skills

JavaScript implements equal-proportion scaling of web page images code and calling method_javascript skills

WBOY
Release: 2016-05-16 17:41:25
Original
891 people have browsed it

When processing web page images, especially in some image list applications, it is difficult to ensure that the image size is uniform. Setting the image size directly will cause the image to stretch and blur the image. The code introduced in this article can automatically scale the image after the image is loaded. Resize the picture.
Javascript:

Copy code The code is as follows:

< script language="javascript " type="text/javascript">
< !--
// Description: Use JavaScript to achieve equal scaling of web page images
// Organizing: 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>

Calling method:
Code:
Copy code The code is as follows:

 The effect after automatic scaling

If the image is large, it is recommended to set the desired image size in the image tag at the same time, so that it does not It will cause the page to expand during loading. This size will not affect the final scaling effect. The above code can be modified as:
Code:
Copy code The code is as follows:

The effect after automatic scaling
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template