谁有图片超出范围就自动缩小的代码??js的。

解决方案 »

  1.   

    按比例缩放
    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>= 130/80)
    {
    if(image.width>130)

    ImgD.width=130;
    ImgD.height=(image.height*130)/image.width;
    }
    else
    {
    ImgD.width=image.width; 
    ImgD.height=image.height;
    }
    //ImgD.alt=image.width+"×"+image.height;
    }
    else
    {
    if(image.height>80)

    ImgD.height=80;
    ImgD.width=(image.width*80)/image.height; 
    }
    else
    {
    ImgD.width=image.width; 
    ImgD.height=image.height;
    }
    //ImgD.alt=image.width+"×"+image.height;
    }
    }
    }
      

  2.   

    <img src="..." onload="autoresize(this)">function autoresize(obj){
      if(obj.width>800)
        obj.width=800;
    }