当前位置:网站首页>原生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)
边栏推荐
- 【Halcon视觉】阈值分割
- RecyclerView最后一条显示不全或显示部分的问题解决
- The problem of incomplete or partial display of the last recyclerview is solved
- Time series anomaly detection
- The difference between equals and = =
- SAP ABAP Netweaver 容器化的一些前沿性研究工作分享
- Study on the basis of opencv
- Uniapp common error [wxml file compilation error]./pages/home/home Wxml and using MySQL front provided by phpstudy to establish an independent MySQL database and a detailed tutorial for independent da
- 数通基础-STP原理
- 【杂谈】Error loading psycopg2 module :No module named psycopg2
猜你喜欢
PLC overview
PLC概述
30分钟彻底弄懂 synchronized 锁升级过程
Learning about opencv (4)
[Halcon vision] programming logic
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
Learning about opencv (2)
[award-winning question] ask Judea pearl, the Turing prize winner and the father of Bayesian networks
AirTest
【Halcon视觉】编程逻辑
随机推荐
【Halcon视觉】形态学腐蚀
议程速递 | 7月27日分论坛议程一览
RecyclerView最后一条显示不全或显示部分的问题解决
Learning about opencv (1)
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
30分钟彻底弄懂 synchronized 锁升级过程
Uniapp error 7 < Map >: marker ID should be a number
【Halcon视觉】编程逻辑
How to use Gmail to pick up / send mail on Foxmail
Review of database -- 3. SQL language
【杂谈】Error loading psycopg2 module :No module named psycopg2
函数模板与同名的非模板函数不可以重载(重载的定义)
数通基础-TCPIP参考模型
Review of database -- 1. Overview
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
数据库的复习--1.概述
【Halcon视觉】图像灰度变化
C language course design Tetris (Part 2)
数通基础-二层交换原理
[Halcon vision] software programming ideas