用javasript判断一个图片的宽度,如果宽度小于一个数值,就以正常显示,大于就设置宽度width属性...


<script language="JavaScript">
<!--
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 400/400){
if(image.width>400){
ImgD.width=400;
ImgD.height=(image.height*400)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>400){
ImgD.height=400;
ImgD.width=(image.width*400)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>


调用:<img src="" border="0" onload="javascript:DrawImage(this);"




<html><head>
<script language="Javascript">
function imgScale(srcobj)
{
var OriginImage=new Image();
OriginImage.src=srcobj.src;
var H=OriginImage.height;
var W=OriginImage.width;
var scale=W>160?W/160:1;//得到缩放比例
var objname=""+srcobj.name
srcobj.width=W/scale;//缩放宽度
srcobj.Height=H/scale;//缩放高度
}
</script>
</head><body>
<img name="newsimg" src="1.jpg" width=160>
<input type=button onclick="imgScale(document.all.newsimg)" value="scale">
</body></html>





<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!--
function View(){
document.all.div1.innerHTML="<img id='img1' src='" + document.all.file1.value + "' onload='zoom()' >"
}
function zoom(){
if( document.all.img1.width>500 ){
document.all.img1.style.zoom=0.5;
}
if(document.all.img1.height > 500){
document.all.img1.style.zoom=0.5;
}
/*if( document.all.img1.width < 250 ){
document.all.img1.style.zoom=2;
}
if(document.all.img1.height < 250){
document.all.img1.style.zoom=2;
}
*/
}
//-->
</SCRIPT>
</HEAD>
</BODY>
<INPUT TYPE="file" id="file1" onchange="View()">
<DIV id="div1"></DIV>
<BODY>
</HTML>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值