IT猫扑网文章教程

分类分类

网页中控制图片缩放代码

2015-06-28 00:00作者:网管联盟

页网中控制图片缩放代码,一般情况下,是很难做到这一点的,因为小图从数据库调用出来的时候小,而当调用的图片是高清的数码照时,你才知道那有多头痛,完全把Tabel挤得不像人样,全挤破了,难看得不敢见人啊,要把图片控制在一定的规格里缩放,我想到的是用JavaScript,终于写出来了,测试了N多回才成功的,请看代码:

  网页前边加(注意JavaScript是区分大小写的,我最不喜欢就他这一点,你们不用注意了,我已经写好测试完了才发出来共享的,直接复制一点问题都没有)

  <script language=&JavaScript&>
  <!--
  //图片按比例缩放
  var flag=false;
  function DrawImage(ImgD){
  var image=new Image();
  var iwidth = 480; //定义允许图片宽度
  var iheight = 530; //定义允许图片高度
  image.src=ImgD.src;
  if(image.width>0 && image.height>0){
  flag=true;
  if(image.width/image.height>= iwidth/iheight){
  if(image.width>iwidth){
  ImgD.width=iwidth;
  ImgD.height=(image.height*iwidth)/image.width;
  }else{
  ImgD.width=image.width;
  ImgD.height=image.height;
  }
  ImgD.alt=image.width+&×&+image.height;
  }
  else{
  if(image.height>iheight){
  ImgD.height=iheight;
  ImgD.width=(image.width*iheight)/image.height;
  }else{
  ImgD.width=image.width;
  ImgD.height=image.height;
  }
  ImgD.alt=image.width+&×&+image.height;
  }
  }
  }
  //调用:
  //-->
  </script>

  在body里  显示图片时

  <img  src=&图片调用路径.jpg&  border=0 >

  OK,就这样搞定了,那不管是小图还是大图,也只能在480X530里游动啦。

  感言:有时候感觉写程序就是那么快乐,好像一切尽在撑握之中,因为又学到了东西。谁有更简单的,请共享下技术吧!

展开全部

相关文章

说两句网友评论
    我要跟贴
    取消