当前位置:网站首页>原生JS-获取transform值 x y z及rotate旋转角度
原生JS-获取transform值 x y z及rotate旋转角度
2022-07-26 10:24:00 【唐策】
CSS3的时代经常会操作transform动画属性,所以也会有想获取transform属性的时候,不过当你去取值时就懵逼了,得出来的是matrix矩阵数值,而且有一点让你恼火,当你操作过z,矩阵的长度与排序就不一样了,你无法固定的取值,(貌似用jQ的一个方法是能固定取到),为了能正常取到这些值,从网上找了一些纯js版的修修改改总算搞定了。
function getTranslate(node,sty){//获取transform值
var translates=document.defaultView.getComputedStyle(node,null).transform.substring(7);
var result = translates.match(/\(([^)]*)\)/);// 正则()内容
var matrix=result?result[1].split(','):translates.split(',');
if(sty=="x" || sty==undefined){
return matrix.length>6?parseFloat(matrix[12]):parseFloat(matrix[4]);
}else if(sty=="y"){
return matrix.length>6?parseFloat(matrix[13]):parseFloat(matrix[5]);
}else if(sty=="z"){
return matrix.length>6?parseFloat(matrix[14]):0;
}else if(sty=="rotate"){
return matrix.length>6?getRotate([parseFloat(matrix[0]),parseFloat(matrix[1]),parseFloat(matrix[4]),parseFloat(matrix[5])]):getRotate(matrix);
}
}
function getRotate(matrix){
var aa=Math.round(180*Math.asin(matrix[0])/ Math.PI);
var bb=Math.round(180*Math.acos(matrix[1])/ Math.PI);
var cc=Math.round(180*Math.asin(matrix[2])/ Math.PI);
var dd=Math.round(180*Math.acos(matrix[3])/ Math.PI);
var deg=0;
if(aa==bb||-aa==bb){
deg=dd;
}else if(-aa+bb==180){
deg=180+cc;
}else if(aa+bb==180){
deg=360-cc||360-dd;
}
return deg>=360?0:deg;
}
/* 第一个参数是元素,第二个是要获取x y z rotate这4个其一
好了,不管你是用translate3d还是translateX、translateY,都能获取到x y z*/
var nowRotate=getTranslate(document.getElementById(“id”),“x”);
console.log(nowRotate)
边栏推荐
猜你喜欢

Flask framework beginner-03-template

Learning about opencv (2)

Beginner of flask framework-04-flask blueprint and code separation

The practice of OpenCV -- bank card number recognition

The difference between equals and = =

Introduction to latex, EPS picture bounding box

如何写一篇百万阅读量的文章

About automatic operation on Web pages

equals与==的区别

3.1 leetcode daily question 6
随机推荐
Meeting OA project (III) -- my meeting (meeting seating and submission for approval)
Use of Android grendao database
2022/07/25------字符串的排列
【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
Learning about tensor (III)
Getting started with SQL - combined tables
Dynamically determine file types through links
Li Kou daily question 917
Redis realizes the correct posture of token bucket
The fourth week of summer vacation
The CLOB field cannot be converted when querying Damon database
Data communication foundation - layer 2 switching principle
The reason why go language is particularly slow to develop run and build commands
详细解析js中的混合方式构造对象(构造加属性,原型加方法)
Prevent XSS attacks
【Halcon视觉】图像的傅里叶变换
关于函数模板描述错误的是(链接格式错误怎么解决)
Vs2019 configuring opencv
Function template parameters (where are the function parameters)
关于模板函数声明与定义的问题[通俗易懂]