JS控制图片大小(自动调整图片大小,图片保持相对尺寸)

    技术2025-08-04  17

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head>

    <body><script language="javascript" type="text/javascript"><!--    var flag=false;    function limitImage(ImgD){        var areaWidth = 110; //你放置图片区域的宽度。       var areaHeight = 120; //你放置图片区域的高度。       var image=new Image();        image.src=ImgD.src;        if(image.width>0 && image.height>0){            flag=true;            if(image.width/image.height>= areaWidth/areaHeight){                if(image.width>areaWidth){                    ImgD.width=areaWidth;                    ImgD.height=(image.height*areaWidth)/image.width;                }else{                    ImgD.width=image.width;                    ImgD.height=image.height;                }                ImgD.alt=image.width+"×"+image.height;            }else{                if(image.height>areaHeight){                    ImgD.height=areaHeight;                    ImgD.width=(image.width*areaHeight)/image.height;                }else{                    ImgD.width=image.width;                    ImgD.height=image.height;                }                ImgD.alt=image.width+"×"+image.height;            }        }    }    //-->    </script><img src="msgbg.gif" border="0" οnlοad="javascript:limitImage(this);" /> </body></html>

    最新回复(0)