简体   繁体   English

旋转时如何获取元素的高度和宽度

[英]How to get height and width of element when it is rotated

As I can get position of element from left side of screen using e.getBoundingClientRect().left因为我可以使用e.getBoundingClientRect().left从屏幕左侧获取元素的位置

When we want to get the position of end points then add width or height according to need but how to get the position of end points when element is rotated.当我们想要获取端点的位置时,根据需要添加宽度或高度,但是如何在元素旋转时获取端点的位置。

Is it possible to get values是否可以获取值

在此处输入图片说明

 let getBoxPos = document.querySelectorAll(".demo") let box1 = document.querySelector("#box1") let box2 = document.querySelector("#box2") let box3 = document.querySelector("#box3") let s = ""; function func() { getBoxPos.forEach(e => { let figXValue = e.getBoundingClientRect().left; s = Number(s) + Number(1); console.log("Start distance for box" + s + ":" + figXValue) }) console.log("End distance for box1:" + (box1.getBoundingClientRect().left + 24)); console.log("End distance for box3:" + (box3.getBoundingClientRect().left + 104)); console.log("Don't know the correct way for box2:Is it box2.getBoundingClientRect().left + 100 or box2.getBoundingClientRect().left + 20 or none of them "); } func();
 .demo { height: 100px; width: 20px; margin-left: 40px; border: 2px solid black; } .demo:nth-child(2) { border: 2px solid red; transform: rotate(-45deg); } .demo:nth-child(3) { border: 2px solid green; transform: rotate(-90deg); }
 <div class="demo" id="box1"></div> <div class="demo" id="box2"></div> <div class="demo" id="box3"></div>

Thanks for help in advance提前感谢您的帮助

What we know: Height , width of element and the angle θ because elements has animation or element is transformed(rotated)我们所知道的:元素的Heightwidth和角度θ因为元素有动画或元素被转换(旋转)

What we don't know or need to find: New width and height of element after rotation我们不知道或需要找到的:旋转后元素的新widthheight

The answer lies in math's答案在于数学

If we know a angle & a side(height, width in this case) of right angle triangle than we can find other sides too.如果我们知道直角三角形的角和边(在这种情况下为高度,宽度),那么我们也可以找到其他边。

To solve a triangle with one side, you also need one of the non-right angled angles.要求解具有一侧的三角形,您还需要一个非直角。 If not, it is impossible:如果没有,那是不可能的:

  1. If you have the hypotenuse, multiply it by sin(θ) to get the length of the side opposite to the angle.如果您有斜边,请将其乘以 sin(θ) 以获得与角度相反的边的长度。
  2. Alternatively, multiply the hypotenuse by cos(θ) to get the side adjacent to the angle.或者,将斜边乘以 cos(θ) 以获得与角度相邻的边。
  3. If you have the non-hypotenuse side adjacent to the angle, divide it by cos(θ) to get the length of the hypotenuse.如果非斜边与角相邻,则将其除以 cos(θ) 即可得到斜边的长度。
  4. Alternatively, multiply this length by tan(θ) to get the length of the side opposite to the angle.或者,将此长度乘以 tan(θ) 以获得与角度相反的边的长度。
  5. If you have an angle and the side opposite to it, you can divide the side length by sin(θ) to get the hypotenuse.如果你有一个角和它的对边,你可以用sin(θ)除以边长得到斜边。
  6. Alternatively, divide the length by tan(θ) to get the length of the side adjacent to the angle.或者,将长度除以 tan(θ) 以获得与角度相邻的边的长度。

Taken from OMNI Calculator 取自 OMNI 计算器

在此处输入图片说明

Only single angle is given but other are found using angle formulas and relations.仅给出单个角度,但使用角度公式和关系找到其他角度。

Now as the question asks: How to get position of blue point than we have to add this to left value现在正如问题所问:如何获得蓝点的位置而不是我们必须将其添加到left

Height*Sinθ + Width*Cosθ

The final code becomes:最终代码变为:

box2.getBoundingClientRect().left + Height*Sinθ + Width*Cosθ

You must include border too if any in height and width, if wanted from border edge如果从边框边缘需要,您也必须包含边框(如果有高度和宽度)

As A Haworth pointed out rotation can be from any point, so not checked the compatibility for every positions but till now it is compatible with rotation within the element.正如A Haworth指出的旋转可以从任何点开始,所以没有检查每个位置的兼容性,但到目前为止它与元素内的旋转兼容。 So use with precaution所以请谨慎使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM