简体   繁体   English

获取旋转div的实际左侧,顶部位置

[英]Obtain the Actual Left,Top position of a rotated div

If I have a div that is rotated 45 degrees and its attributes are: left: 0, top: 0, width: 100px, height: 100px. 如果我有一个旋转45度的div,其属性为:left:0,top:0,width:100px,height:100px。 Can I obtain the actual x,y position of the divs top left corner? 我可以获得div左上角的实际x,y位置吗?

When the div is rotated, the top left corner sits at about -10px,-10px(guess). 当div旋转时,左上角位于约-10px,-10px(猜测)。 But I am attempting to calculate the actual top left corners x,y position for a web application. 但我试图计算Web应用程序的实际左上角x,y位置。

Maybe you know of a mathematical formula that can determine the top left corners x,y position? 也许你知道一个可以确定左上角x,y位置的数学公式? Or maybe a javascript attribute will give me it? 或者也许javascript属性会给我它? Such as div.style.actualLeft;? 比如div.style.actualLeft;?

If it is static 45 degrees and the origin of the rotation is in the center then you'll only have to calculate this once which is: 如果它是静态的45度并且旋转的原点在中心那么你只需要计算一次,这是:

sqrt((width/2)^2+(height/2)^2)
sqrt(50^2+50^2) // Pythagorean theorem

which is 70,71067811865475 这是70,71067811865475

So compared to the origin, x wise it's negative 70,7106... and y wise it's 0. 所以与原点相比,x明智的是负70,7106 ......而y明智的是0。

If you are going to rotate it dynamicly so you must know it all the time then voila! 如果你要动态地旋转它,那么你必须一直都知道它然后瞧! Behold! 看哪!

compute the distance once, sqrt(50^2+50^2) , then multiply this by the rotation you're adding to the cube + 135 degrees (cause it's top left). 计算距离一次, sqrt(50^2+50^2) ,然后乘以你加入立方体的旋转+ 135度(因为它在左上角)。 (90 = straight up, + 45 = 135) (90 =直线上升,+ 45 = 135)

so to get the same answer I just said it would be: 所以为了得到同样的答案,我刚才说:

var dist=Math.sqrt(50^2+50^2);
var degtorad=Math.PI/180;

var x = Math.cos(degtorad * (135+rotation)) * dist;
var y = Math.sin(degtorad * (135+rotation)) * dist;

Now x & y will be relative to the origin. 现在x&y将相对于原点。 If you need more help then be more specific in your question so we can answer better, help us to help you. 如果您需要更多帮助,请在问题中更具体,以便我们能够更好地回答,帮助我们为您提供帮助。

quick calculation: 快速计算:

rotation=45
135+45 = 180

Math.cos(degtorad * 180)) = -1
Math.sin(degtorad * 180)) = 0;

x = -1 * dist = -70,71067811865475
y = 0 * dist = 0

and voila it's like I said for calculating the static in the beginning, I just didn't use any cos / sin for that.. but this works aswell. 瞧,就像我说的那样在开始时计算静态,我只是没有使用任何cos / sin ...但是这也有效。 (-75 works aswell) (-75也适用)

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

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