1.在控制台输入:window.getComputedStyle(element)
> window.getComputedStyle($(".page").eq(1)[0])
得到
<.
transform:"matrix(1, 0, 0, 1, 320, 0)"
transformOrigin:"158px 149px"
transformStyle:"flat"
transition:"-webkit-transform 0.5s ease 0s"
transitionDelay:"0s"
transitionDuration:"0.5s"
transitionProperty:"-webkit-transform"
transitionTimingFunction:"ease"
2.输入element.style.transform
> $(".page").eq(1)[0].style.transform
<. "translate(320px, 0px)"
3.输入$(element).css("transform")
> $(".page").eq(1).css("transform")
<. "matrix(1, 0, 0, 1, 320, 0)"
假设矩阵是:
matrix(a,b,c,d,e,f);
如果只是平移
translate
,只关注最后两个数值就好:
e
是水平移动距离,f
是垂直移动距离如果只是缩放
scale
,只关注a
和d
两个数值就好:
a
是水平缩放倍数,d
是垂直缩放倍数如果是旋转
rotate
,假设旋转角度是θ
:
matrix(cosθ,sinθ,-sinθ,cosθ,0,0)
拉伸
skew
,用到了三角函数tanθ
,只与b
,c
两个参数相关,书写如下:
(注意y
轴倾斜角度在前)
matrix(1,tan(θy),tan(θx),1,0,0)
例如:获取一个rotate角度:
var el = document.getElementById("divTransform");
var st = window.getComputedStyle(el, null);
var tr = st.getPropertyValue("-webkit-transform") ||
st.getPropertyValue("-moz-transform") ||
st.getPropertyValue("-ms-transform") ||
st.getPropertyValue("-o-transform") ||
st.getPropertyValue("transform") ||
"FAIL";
// With rotate(30deg)...
// matrix(0.866025, 0.5, -0.5, 0.866025, 0px, 0px)
console.log('Matrix: ' + tr);
// rotation matrix - http://en.wikipedia.org/wiki/Rotation_matrix
var values = tr.split('(')[1].split(')')[0].split(',');
var a = values[0];
var b = values[1];
var c = values[2];
var d = values[3];
var scale = Math.sqrt(a * a + b * b);
console.log('Scale: ' + scale);
// arc sin, convert from radians to degrees, round
var sin = b / scale;
// next line works for 30deg but not 130deg (returns 50);
// var angle = Math.round(Math.asin(sin) * (180/Math.PI));
var angle = Math.round(Math.atan2(b, a) * (180 / Math.PI));
console.log('Rotate: ' + angle + 'deg');