开发者社区> 问答> 正文

JS控制图片等比例缩放的代码

用js实现图片等比例缩放,一例(js等比例缩放图片 http://www.jbxue.com/article/19127.html)的代码。
1,js代码部分
<SCRIPT language="JavaScript">  
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>


调用方法:
<a href="admin/<? echo $rscase['path']?>" target="_blank"><img src="admin/<? echo $rscase['path']?>" alt="点击放大图片" width="180" height="180" onload='javascript:DrawImage(this,180,);' hspace="3" vspace="3" border="0" /></a>

展开
收起
你的风向标 2014-04-06 08:57:10 7981 0
1 条回答
写回答
取消 提交回答
  • 发现有很多类似的帖子,能做个合集么?或者做成一个脚本调用的工具?
    2014-08-24 11:30:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
JavaScript异步编程 立即下载
Delivering Javascript to World 立即下载
编程语言如何演化-以JS的private为例 立即下载