Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  118] [ 3]  / answers: 1 / hits: 46657  / 13 Years ago, tue, september 27, 2011, 12:00:00

How do I retrieve the width and height properties after I've applied transform: rotate(45deg);?



Like, 11x11 square after rotation becomes 17x17 (Chrome result), but javascript still returns original width/height - 10x10.



How do I get this 17x17?


More From » jquery

 Answers
1

Even if you rotate something the dimensions of it do not change, so you need a wrapper.
Try wrapping your div with another div element and count the wrappers dimensions:



  <style type=text/css>
#wrap {
border:1px solid green;
float:left;
}

#box {
-moz-transform:rotate(120deg);
border:1px solid red;
width:11px;
height:11px;
}
</style>

<script type=text/javascript>
$(document).ready(function() {
alert($('#box').width());
alert($('#wrap').width());
});
</script>
</head>

<body>
<div id=wrap>
<div id=box></div>
</div>
</body>


Redited: the wrapper solution is not working properly, as the wrapper is not automatically adjusted to the contents of the inner div. Follow the mathematical solution:



var rotationAngle;

var x = $('#box').width()*Math.cos(rotationAngle) + $('#box').height()*Math.sin(rotationAngle);

[#89900] Monday, September 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dustin

Total Points: 599
Total Questions: 105
Total Answers: 106

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
dustin questions
;