zoukankan      html  css  js  c++  java
  • 限制网页图片尺寸

    <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>= 140/180){
      if(image.width>140){
      ImgD.width=140;
      ImgD.height=(image.height*140)/image.width;
      }else{
      ImgD.width=image.width;
      ImgD.height=image.height;
      }
      /*ImgD.alt=image.width+"×"+image.height;*/
      }
     else{
      if(image.height>180){
      ImgD.height=180;
      ImgD.width=(image.width*180)/image.height;
      }else{
      ImgD.width=image.width;
      ImgD.height=image.height;
      }
      /*ImgD.alt=image.width+"×"+image.height;*/
      }
     }
     /*else{
     ImgD.src="";
     ImgD.alt=""
     }*/
     }
    </script>
    

      

    把以上这段代码加在<head></head>之间
    然后在图片显示的时候,用这种方式:
    <img src='xxx.gif' border=0 onload='javascript:DrawImage(this);'> (其中xxx.gif是你要显示的图片)

    以上代码我过N次了。。你也可以修改其中的几个数字来达到图片按照你的要求显示(比如你觉得上面这段代码中的图片宽高缩小后还是大了点,你可以再调小一点。其中140与180,分别代表宽和高,你修改的时候一定要注意如果修改140为120,则180一定要修改为160,也就是说要修改就两个值都减去同一个值。总之这两个值相减要等于40..
  • 相关阅读:
    直接插入排序学习笔记
    选择排序学习笔记
    冒泡排序学习笔记
    阿里云ssl证书申请及域名绑定流程
    Nginx user_agent、if指令及全局变量
    rewrite和return笔记
    rewrite和return的简单需求
    Nginx Rewrite正则表达式案例
    linux下WordPress伪静态规则设置
    集群前后端分离(api接口)
  • 原文地址:https://www.cnblogs.com/free3649/p/3757350.html
Copyright © 2011-2022 走看看