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:
< 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:
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: