JavaScript automatically adapts to image size

Source: Internet
Author: User

Blog Source http://blog.csdn.net/eduxh/article/details/1906246

//Image self-adapting size
//ID of the IMG control
//Width and height are the wide height of the IMG parent control-that is, the maximum width and height of the img
//Example: "
functionImgfit (Id,width,height)
{
VarImagearr=document.getElementById (ID);
If(Imagearr.offsetwidth>Width||Imagearr.offsetheight>Height
{
ImageRate1=parseint (Imagearr.offsetwidth)/Width
ImageRate2=parseint (Imagearr.offsetheight)/Height
If(ImageRate2>IMAGERATE1)
ImageArr.style.height=Imagearr.offsetheight/imageRate2+"px";
Else
ImageArr.style.width = imagearr.offsetwidth/imageRate1 +"px";
}
}
To add a center handle:

functionImgfit (Imagearr,width,height)
{
//var Imagearr=document.getelementbyid (ID);
//if (Imagearr.offsetwidth>width | | imagearr.offsetheight>height)
//{
//alert (IMAGEARR.STYLE.MARGINTOP);
Swidth=Imagearr.offsetwidth;
Sheight=Imagearr.offsetheight;
ImageRate1=parseint (Imagearr.offsetwidth)/Width
ImageRate2=parseint (Imagearr.offsetheight)/Height
If(ImageRate2>IMAGERATE1)
{
ImageArr.style.height=Sheight/ImageRate2+"Px";
ImageArr.style.marginLeft=(width-Swidth/IMAGERATE2)/2+"Px";
}
Else
{
ImageArr.style.width=Swidth/ImageRate1+"Px";
ImageArr.style.marginTop=(Height-sheight/imagerate1) /2+ "px"         }   
         //alert ( imagearr.offsetwidth+ ":" +imagearr.offsetheight);
     //}
}

JavaScript automatically adapts to picture size

Related Article

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.