Home > Web Front-end > CSS Tutorial > How Can I Extract the Rotation Value from an Element's CSS Transform Using jQuery?

How Can I Extract the Rotation Value from an Element's CSS Transform Using jQuery?

Barbara Streisand
Release: 2024-12-07 13:47:15
Original
877 people have browsed it

How Can I Extract the Rotation Value from an Element's CSS Transform Using jQuery?

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;
}
Copy after login

Usage:

To retrieve the rotation value of the element with the class myDiv, use:

angle1 = getRotationDegrees($('#myDiv'));
Copy after login

For the last anchor within the element with the class mySpan, use:

angle2 = getRotationDegrees($('.mySpan a:last-child'));
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template