使用JS或Jquery读取CSS缩放值 [英] Read css scale value using JS or Jquery

查看:49
本文介绍了使用JS或Jquery读取CSS缩放值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个div

<div style="transform: scale(2,3)"></div>

我如何获得

transform: scale(x,y) 

使用JS或jQuery的元素的

.

of an element using JS or jQuery.

$('#id').css('transform') 

给我矩阵,但是我需要元素的实际比例值.或者,如果我无法直接获得比例,那么如果我有

gives me the matrix but i need the actual scale values from my element. Or if I cannot get the scale directly then what is the calculation to convert the matrix() to scale if I have

var mt = matrix(....); how do I then convert mt[0] and mt[3] to actual scale values.

推荐答案

var matrixRegex = /matrix\((-?\d*\.?\d+),\s*0,\s*0,\s*(-?\d*\.?\d+),\s*0,\s*0\)/;
var matches = $('#id').css('transform').match(matrixRegex);
console.log(matches)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="id" style="transform: scale(2,3)"></div>

该代码段将返回一个具有原始矩阵的数组,然后索引2和3分别包含X和Y值.归功于Lea Verou

That snippet will return an array with the original matrix, and then indexes 2 and 3 contain X and Y values, respectively. Credit to Lea Verou

这篇关于使用JS或Jquery读取CSS缩放值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆