Retrieving Element Rotation Value using jQuery
In CSS, it's common to apply transformations to elements using the -moz-transform property. However, extracting the specific rotation value from this property using jQuery has been a challenge.
Question:
How to obtain the numerical rotation value applied to a specific HTML element through jQuery, despite it being stored within a complex matrix transformation string?
Answer:
This solution provides a jQuery function that extracts the rotation degrees from the -moz-transform property:
function getRotationDegrees(obj) { var matrix = obj.css("-webkit-transform") || obj.css("-moz-transform") || obj.css("-ms-transform") || obj.css("-o-transform") || obj.css("transform"); if(matrix !== 'none') { var values = matrix.split('(')[1].split(')')[0].split(','); var a = values[0]; var b = values[1]; var angle = Math.round(Math.atan2(b, a) * (180/Math.PI)); } else { var angle = 0; } return (angle < 0) ? angle + 360 : angle; }
Usage:
To retrieve the rotation value of the element with the class myDiv, use:
angle1 = getRotationDegrees($('#myDiv'));
For the last anchor within the element with the class mySpan, use:
angle2 = getRotationDegrees($('.mySpan a:last-child'));
The above is the detailed content of How Can I Extract the Rotation Value from an Element's CSS Transform Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!