CSS3 转换:多重起源?

发布于 2024-12-27 21:30:57 字数 292 浏览 2 评论 0原文

是否可以在CSS3中指定左上角(0%,0%)的原点进行缩放,并指定不同的原点(中心)进行旋转?我只使用 webkit,如果有帮助的话。

我目前正在使用变换列表(即 -webkit-transform:scale(newScale)rotate(newRotate)

但似乎不可能在两次传递之间更改原点。是否有更好的方法来看待这个问题?目前,如果我缩放一个对象并以默认中心为原点旋转它,则该元素的位置现在已关闭,因此当您拖动该元素时,光标仍位于该元素的左上角,而它应该是将原点更改为中心。缩放它可以解决这个问题,但会带来旋转和翻转的新问题。

Is it possible to specify an origin at the top left (0%, 0%) for scaling, and a different origin (center) for rotation in CSS3? I am only working with webkit, if that helps.

I am currently using a transform list (i.e. -webkit-transform: scale(newScale) rotate(newRotate)

but it seems like it isn't possible to change the origin in-between passes. Is there a better way to look at this? Presently, if I scale an object and rotate it with an origin at the default center, the position of the element is now off and so when you drag the element, the cursor is still at the top left of the element, whereas it should be at the center. Changing the origin to the center to scale it fixes this, but presents new problems with rotation and flipping.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2025-01-03 21:30:57

找到了问题的一个很好的解决方案...通过创建父/子关系,如下所示:

<div class="container">
   <img src="" />
</div>

我现在可以设置两个类,如下所示:

.container {
    -webkit-transform-origin: 0% 0%;
    -webkit-transform: scale(0.5);
}

.container img {
    -webkit-transform-origin: 50% 50%;
    -webkit-transform: rotate(45deg);
}

这将完全按照我想要的方式进行:以左上角的原点进行缩放,然后以原点在中心。瞧!

Found a good solution to the problem... by creating a parent/child relationship as follows:

<div class="container">
   <img src="" />
</div>

I can now setup two classes as follows:

.container {
    -webkit-transform-origin: 0% 0%;
    -webkit-transform: scale(0.5);
}

.container img {
    -webkit-transform-origin: 50% 50%;
    -webkit-transform: rotate(45deg);
}

This will do exactly what I want: scale with an origin at the top left, then rotate with the origin at the center. Voila!

把梦留给海 2025-01-03 21:30:57

相反,请将原点 (0,0) 的缩放视为以原点中心进行缩放+平移。单独来看: 与以下

-webkit-transform-origin: top left;
-webkit-transform: scale(1.5);

相同:

-webkit-transform-origin: center;
-webkit-transform: scale(1.5) translate3d(16.66%, 16.66%, 0);

理论上,旋转原点中心应使角突出 sqrt(1/2)-0.5 ,要求我们将原点向下和向右移动 >20.71%,但由于某种原因,webkit 变换算法为我们向下移动了一些东西(但还不够),并为我们缩放了原点(同样不完全)。因此,我们需要向右移动 50% 并针对这种奇怪的行为进行一些调整:

-webkit-transform-origin: center;
-webkit-transform: scale(1.5) rotate(45deg) translate3d(52.5%, 0.5%, 0);
-webkit-transition: all 2s ease-in;

注意:我最初的答案是使用 divwidth:100px;< /code> 和 height100px; 需要 translate3d(54px, 0, 0)

Instead think of the scaling with origin (0,0) as a scaling+translation with origin center. In isolation the following:

-webkit-transform-origin: top left;
-webkit-transform: scale(1.5);

is the same as:

-webkit-transform-origin: center;
-webkit-transform: scale(1.5) translate3d(16.66%, 16.66%, 0);

In theory the rotation origin center should leave the corners sticking out by sqrt(1/2)-0.5 requiring us to move the origin down and right by 20.71%, but for some reason the webkit transform algorithm moves things down for us (but not quite enough) and scales the origin for us (again not quite). Thus we need to move right by 50% and make some adjustments for this odd behavior:

-webkit-transform-origin: center;
-webkit-transform: scale(1.5) rotate(45deg) translate3d(52.5%, 0.5%, 0);
-webkit-transition: all 2s ease-in;

Note: my original answer was using a div with width:100px; and height100px; which requires a translate3d(54px, 0, 0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文