用javasript判断一个图片的宽度,如果宽度小于一个数值,就以正常显示,大于就设置宽度width属性

<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>=     400/400){       
                    if(image.width>400){               
                    ImgD.width=400;       
                    ImgD.height=(image.height*400)/image.width;       
                    }else{       
                    ImgD.width=image.width;               
                    ImgD.height=image.height;       
                    }       
                    ImgD.alt=image.width+"×"+image.height;       
                    }       
                else{       
                    if(image.height>400){               
                    ImgD.height=400;       
                    ImgD.width=(image.width*400)/image.height;                           
                    }else{       
                    ImgD.width=image.width;               
                    ImgD.height=image.height;       
                    }       
                    ImgD.alt=image.width+"×"+image.height;       
                    }       
                }       
  }           
  //-->       
  </script>   
    
    
  调用:<img   src=""   border="0"   onload="javascript:DrawImage(this);"



<html><head>   
  <script   language="Javascript">   
  function   imgScale(srcobj)   
  {   
      var   OriginImage=new   Image();   
      OriginImage.src=srcobj.src;   
      var   H=OriginImage.height;   
      var   W=OriginImage.width;   
      var   scale=W>160?W/160:1;//得到缩放比例   
      var   objname=""+srcobj.name   
      srcobj.width=W/scale;//缩放宽度   
      srcobj.Height=H/scale;//缩放高度   
  }   
  </script>   
  </head><body>   
  <img   name="newsimg"   src="1.jpg"   width=160>   
  <input   type=button   onclick="imgScale(document.all.newsimg)"   value="scale">   
  </body></html>   
  


    
  <HTML>   
  <HEAD>   
  <SCRIPT   LANGUAGE="JavaScript">   
  <!--   
  function   View(){   
  document.all.div1.innerHTML="<img   id='img1'   src='"   +   document.all.file1.value   +   "'   onload='zoom()'   >"   
  }   
  function   zoom(){   
  if(   document.all.img1.width>500   ){   
  document.all.img1.style.zoom=0.5;   
  }   
  if(document.all.img1.height   >   500){   
  document.all.img1.style.zoom=0.5;   
  }   
  /*if(   document.all.img1.width   <   250   ){   
  document.all.img1.style.zoom=2;   
  }   
  if(document.all.img1.height   <   250){   
  document.all.img1.style.zoom=2;   
  }   
  */   
  }   
  //-->   
  </SCRIPT>   
  </HEAD>   
  </BODY>   
  <INPUT   TYPE="file"   id="file1"   onchange="View()">   
  <DIV   id="div1"></DIV>   
  <BODY>   
  </HTML>   

你可能感兴趣的:(javasript)