获取元素的缩放值? [英] Get the scale value of an element?

查看:125
本文介绍了获取元素的缩放值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何获得元素的缩放值。

I'm wondering how I can get the scale value of an element?

我试过 $(element).css -webkit-transform'); 它返回 matrix(scaleX,0,0,scaleY,0,0); scaleX scaleY

推荐答案

如果它是由矩阵指定的,我猜你不能用一个简单的方法,但你可以很容易地解析该值:

If it was specified by a matrix I guess you can't with a straightforward way, but you can easily parse the value:

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

matches [1] 将包含scaleX和 matches [2] 将包含scaleY。如果可能还应用了其他转换,则需要稍微调整正则表达式,因为现在假设所有其他参数都为0。

matches[1] will contain scaleX and matches[2] will contain scaleY. If it's possible that other transformations have also been applied, you'd need to slightly tweak the regex, because now it assumes that all other parameters are 0.

一种方法只是获取比例值可能是删除任何变换,测量元素的计算宽度/高度,然后将它们添加回来并再次测量。然后划分新/旧值。没有尝试过,但它可能工作。 jQuery本身对许多测量使用类似的方法,它甚至有一个未记录的 $。swap()函数。

A way to just get the scale values might be to remove any transforms, measure the computed width/height of the element and then add them back and measure again. Then divide new/old values. Haven't tried it, but it might work. jQuery itself uses a similar approach for many measurements, it even has an undocumented $.swap() function just for this.

PS:您正在使用 -o-transform -moz-transform ms-transform 太,对吗?

PS: You are using -o-transform -moz-transform and -ms-transform too, right?

这篇关于获取元素的缩放值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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