我想知道如何在旋转元素时获取其原始尺寸,当我使用 transform: rotate()
并尝试通过 element.getBoundingClientRect()
获取旋转后的尺寸时,它返回不同的 宽度 和 高度,有可能获得旋转元素的真实尺寸吗?
let div1 = document.getElementById('one')
let div2 = document.getElementById('two')
// Here the width and height is 50 and 80
console.log(div1.getBoundingClientRect())
// Here because i used transform rotate the width is 86 and height 94
console.log(div2.getBoundingClientRect())
div#one {
width: 50px;
height: 80px;
background-color: red;
}
div#two {
width: 50px;
height: 80px;
background-color: red;
transform: rotate(35deg);
}
<div id="one"></div>
<br/>
<div id="two"></div>
getComputedStyle(element)["height"]
和getComputedStyle(element)["width"]
是一个潜在的选项。 - Nisala