onload="ResizeImage(this,220,200);

 

function ResizeImage(obj, MaxW, MaxH) {

        if (obj != null) imageObject = obj;

        var state = imageObject.readyState;

        if (state != 'complete') {

            setTimeout("ResizeImage(null," + MaxW + "," + MaxH + ")", 50);

            return;

        }

        var oldImage = new Image();

        oldImage.src = imageObject.src;

        var dW = oldImage.width;

        var dH = oldImage.height;

        if (dW > MaxW || dH > MaxH) { a = dW / MaxW; b = dH / MaxH; if (b > a) a = b; dW = dW / a; dH = dH / a; }

        if (dW > 0 && dH > 0) { imageObject.width = dW; imageObject.height = dH; }

    }

 

 

 

 

方法二

<script>
    var imageArr=document.getElementById(controlID);
    var imageRate = imageArr.offsetWidth / imageArr.offsetHeight;   
   
    if(imageArr.offsetWidth > maxWidth)
    {
        imageArr.style.width=maxWidth + "px";
        imageArr.style.Height=maxWidth / imageRate + "px";
    }
   
    if(imageArr.offsetHeight > maxHeight)
    {
        imageArr.style.width = maxHeight * imageRate + "px";
        imageArr.style.Height = maxHeight + "px";
    }

js 图片自适应
</script>
js 图片自适应

相关文章:

  • 2021-08-23
  • 2021-05-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2021-09-13
猜你喜欢
  • 2021-10-17
  • 2021-07-16
  • 2021-12-12
  • 2022-12-23
  • 2021-11-06
  • 2022-02-07
  • 2021-06-23
相关资源
相似解决方案