简体   繁体   English

转换原点更改延迟

[英]Transform origin change delay

I have a problem where browser is not aware of transform-origin change to dom element or better say it is aware but with delay. 我有一个问题,浏览器不知道对dom元素的转换起源更改,或者更好的说它知道但有延迟。

I have a placeholder which i want to scale in on click. 我有一个占位符,我想按一下放大。 I have this piece of code where i calculate clicked element center and apply transform origin to transformed element. 我有这段代码,我在其中计算单击的元素中心并将变换原点应用于变换后的元素。

$(".place-holder")
        .css({
            "-webkit-transform-origin":transCenterY+"px "+transCenterX+"px",
            "transform-origin":transCenterY+"px "+transCenterX+"px",
            "transform":"scale(2)"
        });

What happens is that browser starts to scale to transform-origin: 50% 50% and in the middle of animation it shifts to correct transform-origin position. 发生的情况是浏览器开始按比例缩放到变换原点:50%50%,并且在动画中间,它移动到正确的变换原点位置。

If i apply scale 500 milliseconds after applied transform-origin the animation works. 如果我在应用了变换原点后500毫秒应用了比例,则动画将起作用。

This code works smoothly: 该代码可以正常运行:

$(".place-holder")
        .css({
            "-webkit-transform-origin":transCenterY+"px "+transCenterX+"px",
            "transform-origin":transCenterY+"px "+transCenterX+"px"
        });

    setTimeout(function(){
        $(".place-holder").css("transform","scale(2)");
    }, 500)

Here is the fiddle so you can see what is going on: https://jsfiddle.net/r9xboa1b/ 这是小提琴,因此您可以查看发生了什么: https : //jsfiddle.net/r9xboa1b/

Click on the red squares - see the shift during transition? 单击红色方块-看到过渡期间的转变了吗? You can only click once and then run fiddle again. 您只能单击一次,然后再次运行小提琴。

If you apply scale in timeout the shift is gone. 如果在超时中应用刻度,则移位将消失。

EDIT 编辑

What i actually want to achieve is smooth transition to the center of the each square without shifting and without timeout. 我真正想要实现的是平滑过渡到每个正方形的中心,而不会发生移位且没有超时。 https://jsfiddle.net/r9xboa1b/ https://jsfiddle.net/r9xboa1b/

I resolved the issue. 我解决了这个问题。

The trick was, that i applied transition to all transformations on element. 诀窍是,我将过渡应用于元素上的所有转换。 Therefore the change in transform-origin was also transitioning 1s - and that explains the shift in animation. 因此,变换原点的变化也过渡了1s,这可以解释动画的变化。

So changing the 所以改变

transition:all 1s;

to

transition:transform 1s;

did the trick. 做到了。

For others that come to this problem - apply transition to only those transform properties you need. 对于其他遇到此问题的人-仅将转换应用于所需的那些转换属性。

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

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