简体   繁体   English

检索css3缩放元素的宽度/高度

[英]Retrieve width/height of a css3 scaled element

I'm fighting against an oddity (I think) of the offsetWidth property. 我正在反对offsetWidth属性的奇怪(我认为)。
this is the scenario: 这是场景:

I've got, let's say, a span tag, in my js, at a certain point I perform a css3 transform to this element, like: 我已经得到了一个span标签,在我的js中,在某一点上我执行css3转换到这个元素,如:

        el.set('styles', {
            'transform':'scale('+scale+', '+scale+')',      /* As things would be in a normal world */
            '-ms-transform':'scale('+scale+', '+scale+')',      /* IE 9 */
            '-moz-transform':'scale('+scale+', '+scale+')',         /* Moz */
            '-webkit-transform':'scale('+scale+', '+scale+')',  /* Safari / Chrome */
            '-o-transform':'scale('+scale+')'       /* Oprah Winfrey */ 
        }); 
        w = el.getWidth();
        console.log('after scaling: ' + w);

at this point the log returns me fuzzy values, like it doesn't know what to say. 此时日志返回模糊值,就像它不知道该说什么。
any suggestion? 有什么建议吗?

getBoundingClientRect() returns the correct values for me. getBoundingClientRect()为我返回正确的值。

jsFiddle . jsFiddle

Transforms don't affect the intrinsic CSS properties, so you are seeing correct behavior. 转换不会影响内在的CSS属性,因此您可以看到正确的行为。 You need to look at the Current Transformation Matrix - as returned from getComputedStyle().webkitTransform in order to figure out how big something has been scaled. 你需要查看当前转换矩阵 - 从getComputedStyle()。webkitTransform返回,以便弄清楚有多大的东西被缩放。

Update: In Firefox 12 and later and Chrome/Safari - as Alex says below, you can use getBoundingClientRect() to take into account any transforms applied to an element 更新:在Firefox 12及更高版本和Chrome / Safari中 - 正如Alex在下面所述,您可以使用getBoundingClientRect()来考虑应用于元素的任何变换

The scale transition starts from 50%/50% because that's the default & correct behavior. 规模转换从50%/ 50%开始,因为这是默认和正确的行为。 If you want it to start from the origin, then you need to set transform-origin: 0% 0%; 如果你想让它从原点开始,那么你需要设置transform-origin:0%0%;

I take it the span tag has display:block ? 我认为span标签有display:block transforms are only supposed to work for block elements. 转换只适用于块元素。 When I goto console and load jquery (just for ease) and do this: 当我转到控制台并加载jquery(只是为了方便)并执行此操作:

$('span#foo').css({"-webkit-transform": "scale(2.0, 2.0)"})

nothing happens. 什么都没发生。 But if I do this: 但如果我这样做:

$('span#foo').css('display','block').css({"-webkit-transform": "scale(2.0, 2.0)"})

suddenly it changes, and offsetHeight increases. 突然它变了,offsetHeight增加了。 Disappointingly, the offset hight goes from 16 to 19, and not to 32 (although the visual appearance is double the size and maybe it is accurate) — but at least it does appear to work as advertised. 令人失望的是,偏移高度从16变为19,而不是32(尽管视觉外观是尺寸的两倍并且可能是准确的) - 但至少它确实像宣传的那样工作。

getBoundingClientRect() didn't work for me (in Chrome 49). getBoundingClientRect()对我不起作用(在Chrome 49中)。

This answer led me to another solution to my problem: https://stackoverflow.com/a/7894886/1699320 这个答案让我解决了我的问题: https//stackoverflow.com/a/7894886/1699320

function getStyleProp(elem, prop){
    if(window.getComputedStyle)
        return window.getComputedStyle(elem, null).getPropertyValue(prop);
    else if(elem.currentStyle) return elem.currentStyle[prop]; //IE
}
getStyleProp(element, 'zoom')    //gives zoom factor to use in calculations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM