最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how can we get new image size dimension after giving object-fit:contain property to the image tag? - Stack Overflow

programmeradmin5浏览0评论

enter image description here

In the above image, you can see the original image size and image get shrink after giving the property "object-fit:contain".

can we calculate the effective image size after giving "Object-fit:contain" property.

enter image description here

In the above image, you can see the original image size and image get shrink after giving the property "object-fit:contain".

can we calculate the effective image size after giving "Object-fit:contain" property.

Share Improve this question asked Sep 5, 2018 at 13:34 pratik solankipratik solanki 1231 silver badge3 bronze badges 1
  • css will adapt to the highest image . If you use javascript to find out which is the lowest , object-fit:contain will leave a gap on either sides of the higest (you get the opposite behavior) . clipping could be an option. height : 100% is also the reason . Can you set a full working snippet and clarify your needs/issue. – G-Cyrillus Commented Sep 5, 2018 at 14:28
Add a ment  | 

2 Answers 2

Reset to default 20

The image object after loading contains both the displayed dimensions of the container and the dimensions of the original image. So the calculations are fairly simple:

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/wvbpcjhk/ ]

You can get the dimensions of an element through JavaScript with a few methods:

Total Displayed Area - measures the displayed area the content with all additional features takes up like margins, borders, etc.

element.offsetHeight;
element.offsetWidth; 

Displayed Content - measures the content visible (padding included).

element.clientHeight;
element.clientWidth;

Total Content - measures the total content visible and hidden (padding included).

element.scrollHeight;
element.scrollWidth;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论