Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get element -moz-transform:rotate value in jQuery

I have CSS style for a layer:

.element {     -webkit-transform: rotate(7.5deg);          -moz-transform: rotate(7.5deg);           -ms-transform: rotate(7.5deg);            -o-transform: rotate(7.5deg);              transform: rotate(7.5deg); } 

Is there a way to get curent rotation value through jQuery?

I tried this

$('.element').css("-moz-transform") 

The result is matrix(0.991445, 0.130526, -0.130526, 0.991445, 0px, 0px) which doesn't tell me a lot. What I'm looking to get is 7.5.

like image 479
Goldie Avatar asked Nov 25 '11 14:11

Goldie


People also ask

How get data attribute value in jQuery?

Answer: Use the jQuery attr() Method You can simply use the jQuery attr() method to find the data-id attribute of an HTML element.

What does VAL () do in jQuery?

val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined . When the first element in the collection is a select-multiple (i.e., a select element with the multiple attribute set), .

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

Here's my solution using jQuery.

This returns a numerical value corresponding to the rotation applied to any HTML element.

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; }  angle1 = getRotationDegrees($('#myDiv')); angle2 = getRotationDegrees($('.mySpan a:last-child')); 

etc...

like image 89
TwystO Avatar answered Sep 27 '22 23:09

TwystO