用JS自动缩小超出大小的图片
作者: Qiuss | 来源: | 点击: | 发布时间: 2009-12-30
JavaScript Code复制内容到剪贴板
- <SCRIPT src="/js/ShowPic.js" type=text/javascript></SCRIPT>
- <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>= 200/200){
- if(image.width>200){
- ImgD.width=200;
- ImgD.height=(image.height*200)/image.width;
- }else{
- ImgD.width=image.width;
- ImgD.height=image.height;
- }
- }
- else{
- if(image.height>200){
- ImgD.height=200;
- ImgD.width=(image.width*200)/image.height;
- }else{
- ImgD.width=image.width;
- ImgD.height=image.height;
- }
- }
- }
- }
- //-->
- </script>
把上边的这段代码加入<head></head>之中
在相关图片上加入以下代码或者把onload的代码加入到body里:
XML/HTML Code复制内容到剪贴板
- <img src=# onload=javascript:DrawImage(this); >
