Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting CSS left and top when div is rotated

I'm trying to get the style.left and style.top of a rectangular div, after it has been rotated using style.transform=rotate(90deg).

I understand how the div is being rotated, with it being rotated around a 'transform point'. And I also understand that a div could be rotated by 45 degrees, so giving the new top/left of that would be awkward (In effect giving the bounding box left/top).

But back to the original question, rotating the rectangular div by 90 degrees, is there a way to get the 'new' left/top?

The reason I need this, is for a project im working on to upload images, allow the user to zoom, rotate etc, but currently having to do it with PHP to keep all the dimensions correct for the final image (Which is obviously bad, because I'm having to keep loading a new image once PHP has done the rotating/zooming etc)

I've also made a little jsfiddle showing that the top/left position doesn't change when it is rotated

like image 600
Just Lucky Really Avatar asked Dec 19 '25 01:12

Just Lucky Really


1 Answers

Okay, thanks to the comment left above, I managed to throw together an answer.

Basically using:

newleft = parseInt(div.style.top) + Math.cos(90) * parseInt(div.style.height);
newtop = parseInt(div.style.left) + Math.sin(90) * parseInt(div.style.height);

after the div had been rotated.

I've updated my jsfiddle aswell, because the one in the comment above uses jQuery, but this way uses only javascript.

like image 170
Just Lucky Really Avatar answered Dec 20 '25 15:12

Just Lucky Really



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!