css 行内元素转换_CSS转换后获取元素尺寸

css 行内元素转换

I've been playing a lot with meta viewports recently due to seeing many HTML5 TV apps coded explicitly for 1280x720 which you'll see on many HD televisions.  We all know that it's a much better practice to use responsive design than hardcode dimensions but, that aside, meta viewports are meant to do the scaling.  So it's CSS transforms, specifically scale(), to the rescue.

我最近在meta视口中玩了很多,这是因为看到许多HTML5电视应用程序明确编码为1280x720,而您会在许多高清电视上看到它们。 我们都知道,使用响应式设计比使用硬编码尺寸要好得多,但是,此外,元视口还可以进行缩放。 因此,这是CSS转换,特别是scale()到了救援。

In creating a meta viewport shim, I needed to calculate an element's dimensions after it had been scaled.  Properties like clientWidth and innerWidth will return the element's original width, ignoring the transform.  To get the scaled size you must use getBoundingClientRect:

在创建元视口填充器时,我需要在缩放元素后计算元素的尺寸。 诸如clientWidthinnerWidth类的属性将返回元素的原始宽度,而忽略转换。 要获得缩放的大小,必须使用getBoundingClientRect


var originalWidth = myElement.innerWidth; // 1280
var originalHeight = myElement.innerHeight; // 720

originalElement.style.transform = 'scale(1.5)';

console.log(originalElement.getBoundingClientRect());

/*
ClientRect {
  bottom: 1080
  height: 1080
  left: 0
  right: 1920
  top: 0
  width: 1920
}
*/


The example above sets the scale and returns different desired height and width dimensions based on the scale.  getBoundingClientRect returns more than just height and width by position coordinates as well.

上面的示例设置比例尺,并根据该比例尺返回不同的所需高度和宽度尺寸。 getBoundingClientRect返回高度和宽度,还返回位置坐标。

I was worried I wouldn't be able to accomplish this feat but getBoundingClientRect was the perfect solution!

我担心我无法完成这项壮举,但是getBoundingClientRect是完美的解决方案!

翻译自: https://davidwalsh.name/get-element-dimensions-css-transform

css 行内元素转换

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值