Javascript 不调整图像大小

Javascript 不调整图像大小,javascript,jquery,image-resizing,Javascript,Jquery,Image Resizing,这是我的网址 此图像的高度和宽度为225和400 我需要100的高度和宽度 下面是javascript函数 var myUrl = 'https://firebasestorage.googleapis.com/v0/b/apartments-ea9e5.appspot.com/o/bulletinboard%2Fsample-1494577677180.jpg?alt=media&token=7a0c1ff0-1bc7-4ea9-9176-072aacc4349c'; va

这是我的网址

此图像的高度和宽度为225和400

我需要100的高度和宽度

下面是javascript函数

var myUrl = 'https://firebasestorage.googleapis.com/v0/b/apartments-ea9e5.appspot.com/o/bulletinboard%2Fsample-1494577677180.jpg?alt=media&token=7a0c1ff0-1bc7-4ea9-9176-072aacc4349c';

    var img = new Image();
    img.src = myUrl;
    var width;
    var height;
    img.addEventListener("load", function(){
         console.log( this.naturalWidth +' '+ this.naturalHeight )
         var imagewidth=this.naturalWidth;
         var imageheight=this.naturalHeight;
         var maxht =100;
         var maxwt = 100;
         if (imageheight > maxht || imagewidth > maxwt) 
         {
           var old_ratio = imageheight / imagewidth;
           var min_ratio = maxht / maxwt;
            // If it can scale perfectly.
            if (old_ratio === min_ratio) {
              // this.resize_image(img, maxht, maxwt);
              console.log(old_ratio);
              console.log(min_ratio);
              img.height = maxht;
              img.width  = maxwt;
              console.log("does not change height and width");
              console.log(img.src);
            }
             else {
            var newdim = [imageheight, imagewidth];
            newdim[0] = maxht;  // Sort out the height first
            // ratio = ht / wt => wt = ht / ratio.
            var old_ratio = imageheight / imagewidth;
            newdim[1] = newdim[0] / old_ratio;
            // Do we still have to sort out the width?
            if (newdim[1] > maxwt) {
              newdim[1] = maxwt;
              newdim[0] = newdim[1] * old_ratio;
            }
            //this.resize_image(img, newdim[0], newdim[1]);
            img.height = newdim[0];
            img.width  = newdim[1];
            console.log("change heigth and width");
            console.log(img.src);
            }


         }


         // width=this.naturalWidth;
         // height=this.naturalHeight;
    });
但不会改变高度和宽度

请给我提个建议,
谢谢

您可以仅使用css属性来完成此操作。下面是一个代码示例

希望它会有用

img{
最大宽度:100px;
最大高度:100px;
}

您可以使用CSS调整图像大小,如下所示:

img {
    width: 100px;
    height: 100px;
}
resizeIMG(100, 100);
总是先用CSS,如果你不能用这种方法解决它,那么就找一种JavaScript方法来解决它

在JavaScript中,通过这种方式(例如),您可以根据需要优化代码:

function resizeIMG(w, h){
  var img = document.getElementsByTagName('img'), i=0;

  while(img.length > i) {
    img[i].style.width = w + 'px';
    img[i].style.height = h + 'px';
    i++;
  }
}
这样称呼它:

img {
    width: 100px;
    height: 100px;
}
resizeIMG(100, 100);
函数大小调整(w,h){
var img=document.getElementsByTagName('img'),
i=0;
而(img.length>i){
img[i].style.width=w+'px';
img[i].style.height=h+'px';
i++;
}
}
调整尺寸(100100)

您可以通过编写这个简单的结构和css来实现这一点,而无需拉伸图像

.box{
宽度:100px;
高度:100px;
溢出:隐藏;
显示:内联块;
}
img{
最大宽度:100%;
高度:自动;
}


那你为什么不使用CSS或调整图像大小呢?谢谢你的回复。我更改了CSS,但没有正确地编写,我为你这样做了CSS和Javascript。。。请看答案谢谢。我需要移动设备上的响应图像。我有多个图像。因此我需要调整图像大小。谢谢。但我有多个图像。显示在具有响应的移动设备上。因为我需要图像高度相同。我还需要在css上书写。宽度:100%;heigth:autoMan,那么你的问题不清楚了,你说你的身高和宽度在100米,可以通过一些额外的线条来完善你的工作。。。让我把它和你的代码比较一下。谢谢你的病人。请参考这个。