【问题标题】:How to get scaled height of an image from known width of an html container?如何从 html 容器的已知宽度获取图像的缩放高度?
【发布时间】:2022-12-01 00:34:03
【问题描述】:

我在我的 html 页面上面临挑战,要在动态宽度已知的容器中找到图像的缩放高度

让宽度为 var wid = 740;对于桌面,它可能会因屏幕尺寸而异,但该值在某些 js 函数中是已知的。

假设我上传了一张尺寸为 600x591 的图片,它将缩放到宽度为 740 的容器,我需要的是动态找到缩放后的高度值 在上面的示例中,缩放后的高度值为 729,我知道的值是原始图像大小 600 x 591 和容器宽度 740(这里图像是按比例放大的)

另一个例子是大小为 1170 x 574 的图像,它被缩放到 740 x 363 ,所以我需要从图像分辨率 (1170 x 574) 和 740 的容器动态地找到值 363

var wid    = document.getElementById('wrapid').offsetWidth; // wrapper width
var width  = imageWidth;
var height = imageHeight;
var hei    = 0; // this is for wrapper scaled height , need a calulation to find this

注意:带有的容器可能会因屏幕尺寸而异,但它将是一个常数。

任何帮助将不胜感激,提前致谢

【问题讨论】:

    标签: scale image-scaling image-size javascipt


    【解决方案1】:

    得到了一个解决方案,在下面添加

    hei = Math.round( (wid*height) / width );

    【讨论】:

      猜你喜欢
      • 2012-08-04
      • 2012-03-23
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2012-11-05
      • 2014-10-20
      • 2019-05-20
      • 1970-01-01
      相关资源
      最近更新 更多