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

本文介绍了一种通过JavaScript实现的图片自适应显示方法,确保图片能在指定区域内按比例缩放而不失真。此方法适用于固定尺寸的展示区域,能够根据图片原始尺寸调整其宽高比。

<!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" onload="javascript:limitImage(this);" />
</body>
</html>

js等比例缩放页面图片大小 // JavaScript Document function handle_img(tagimg_name,tagimg_width,tagimg_height){ var img_object=new Image(); var get_imgs=document.getElementsByName(tagimg_name); for(var i=0 ; i<get_imgs.length ; i++){ img_object.src=get_imgs[i].src; //上传的图片宽高均在tagimg_width和tagimg_height以内。则原图片显示 if(img_object.width<=tagimg_width && img_object.height<=tagimg_height){ get_imgs[i].height=img_object.height; get_imgs[i].width=img_object.width; } //如果宽度超过了tagimg_width if(img_object.width>tagimg_width){ if(tagimg_width/img_object.width*img_object.height>tagimg_height){ var w_width=tagimg_width/img_object.width*img_object.height; get_imgs[i].width=(tagimg_height/w_width)*tagimg_width; get_imgs[i].height=tagimg_height; }else{ get_imgs[i].height=tagimg_width/img_object.width*img_object.height; get_imgs[i].width=tagimg_width; } } //如果高度超过了tagimg_height if(img_object.height>tagimg_height){ if(tagimg_height/img_object.height*img_object.width>tagimg_width){ var h_height=tagimg_height/img_object.height*img_object.width; get_imgs[i].height=(tagimg_width/h_height)*tagimg_height; get_imgs[i].width=tagimg_width; }else{ get_imgs[i].width=tagimg_height/img_object.height*img_object.width; get_imgs[i].height=tagimg_height; } } } } //handle_img(tagimg_name,tagimg_width,tagimg_height) 页面加载完毕后调用该函数即可 //第一个参数为 页面当中img标记的name值 //第二个参数为 将要设定预览图片的宽度 //第三个参数为 将要设定预览图片的高度
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值