余志国网站设计工作室是专业从事义乌网站建设、义乌外贸网站建设、义乌网站制作、义乌网站设计的专业义乌网站建设工作室,是义乌外贸网站建设专家!

图片自动按宽高比例缩小的js代码

分类:程序开发 Tags: 图片 javascript 评论:0 浏览:4052 最近更新时间:2010/7/16 13:24:00

 
  1. <script type="text/javascript">
  2. function setImgSize(img,width,height){
  3. var MaxWidth=width;//设置图片宽度界限
  4. var MaxHeight=height;//设置图片高度界限
  5. var HeightWidth=img.offsetHeight/img.offsetWidth;//设置高宽比
  6. var WidthHeight=img.offsetWidth/img.offsetHeight;//设置宽高比
  7. if(img.offsetWidth>MaxWidth){
  8. img.width=MaxWidth;
  9. img.height=MaxWidth*HeightWidth;
  10. }
  11. if(img.offsetHeight>MaxHeight){
  12. img.height=MaxHeight;
  13. img.width=MaxHeight*WidthHeight;
  14. }
  15. }
  16. </script>
 
  1. <img src="/pic/images4/20100716232307734.gif" border=0  onload="setImgSize(this,150,150);"
 
  1. //按比例缩小图片
  2. //调用方式onload="setImgSize(this,150,150)"
  3.  
  4. var flag=false;
  5. function setImgSize(ImgD,Imgwidth,Imgheight){
  6. var image=new Image();
  7. image.src=ImgD.src;
  8. if(image.width>0 && image.height>0){
  9. flag=true;
  10. if(image.width/image.height>= 1){
  11. if(image.width>Imgwidth){ 
  12. ImgD.width=Imgwidth;
  13. ImgD.height=(image.height*Imgwidth)/image.width;
  14. }else{
  15. ImgD.width=image.width; 
  16. ImgD.height=image.height;
  17. }
  18. }
  19. else{
  20. if(image.height>Imgheight){ 
  21. ImgD.height=Imgheight;
  22. ImgD.width=(image.width*Imgheight)/image.height; 
  23. }else{
  24. ImgD.width=image.width; 
  25. ImgD.height=image.height;
  26. }
  27. }
  28. }

本文地址:/478/
  • 相关文章
  • 推荐文章
  • 最新文章
发表评论





您的IP:18.97.14.91,来自:

留言

 


免费咨询
建站咨询热线
15967985565
QQ: 358758308 Skype: yuzhiguo Facebook: yuzhiguo Email