在jQuery中获取元素-moz-transform:旋转值

2022-01-16 00:00:00 rotation transform jquery css

我有一个图层的 CSS 样式:

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);
}

有没有办法通过 jQuery 获取当前的旋转值?

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

我试过了

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

结果是 matrix(0.991445, 0.130526, -0.130526, 0.991445, 0px, 0px) 这并没有告诉我很多.我想要的是 7.5.

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.

推荐答案

这是我使用 jQuery 的解决方案.

Here's my solution using jQuery.

这会返回一个数值,对应于应用于任何 HTML 元素的旋转.

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'));

等等……

相关文章