How to get actual (not original) height of a CSS-rotated element

青春壹個敷衍的年華 提交于 2019-12-02 21:00:31
recursive

Dusting off my high school geometry and my formidable graphics skills, I put this diagram together. If you have variables width, height, and rotation in javascript, you could express the height this way:

var rotated_height = width * Math.sin(rotation) + height * Math.cos(rotation);

If you're only going to be using rotations of 45 degrees and the width and height are always equal, you can calculate the actual rotated height by using the relatively simple mathematical Hypotenuse formula:

which states that the square of the length of the hypotenuse equals the sum of the squares of the lengths of the other two sides.

In this case, the longest side would be the diagonal from corner to opposing corner. This will roughly translate into jQuery like so:

Math.sqrt(Math.pow($('#diamond').height(), 2) + Math.pow($('#diamond').width(), 2))

See the updated jsFiddle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!