如何在赋予图像标签对象-FIT:CONTAINE属性后获得新的图像尺寸尺寸? [英] how can we get new image size dimension after giving object-fit:contain property to the image tag?

查看:50
本文介绍了如何在赋予图像标签对象-FIT:CONTAINE属性后获得新的图像尺寸尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

enter image description here

在上图中,您可以看到原始图像的大小以及在赋予属性"Object-Fit:Container"后图像变小。

给出"Object-Fit:Container"属性后,我们是否可以计算有效的图像大小。

推荐答案

加载后的图像对象既包含容器的显示尺寸,也包含原始图像的尺寸。因此,计算相当简单:

function getContainedSize(img) {
  var ratio = img.naturalWidth/img.naturalHeight
  var width = img.height*ratio
  var height = img.height
  if (width > img.width) {
    width = img.width
    height = img.width/ratio
  }
  return [width, height]
}

[http://jsfiddle.net/wvbpcjhk/]

这篇关于如何在赋予图像标签对象-FIT:CONTAINE属性后获得新的图像尺寸尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆