您如何计算两个旋转的两个元素之间的x和y偏移,但具有不同的变换原点位置?

发布于 2025-01-28 06:25:11 字数 886 浏览 2 评论 0原文

我想知道,我们如何计算两个相同旋转的两个相等元素之间的x和y偏移,但具有不同的变换原点位置。

我创建了一个示例片段,以更好地说明问题。

有两个150x100 PX矩形,一个是在0%0%的变换起源位置旋转,而另一个则为50%50%。在特定旋转时计算这两个矩形之间的偏移的公式是什么?

.wrapper {
  position: relative;
  background: silver;
  margin: 150px;
  width: 150px;
  height: 100px;
}

.div-1, .div-2 {
  position: absolute;
  top: 0;
  left: 0;
  width: 150px;
  height: 100px;
  border: 1px solid black;
  animation: rotate 30s linear infinite;
}
.div-1 {
  transform-origin: top left;
}
.div-2 {
  transform-origin: center center;
}

@keyframes rotate {
  100% {
    transform: rotate(360deg);
  }
}
<div class="wrapper">
  <div class="div-1"></div>
  <div class="div-2"></div>
</div>

I would like to know, how can we calculate the X and Y offset between two equal elements with the same rotation, but with a different transform origin positions.

I've created an example snippet to help better illustrate the problem.

There are two 150x100 px rectangles, one is being rotated at the transform origin position of 0% 0%, while other at 50% 50%. What would be the formula for calculating the offset between these two rectangles at specific rotation?

.wrapper {
  position: relative;
  background: silver;
  margin: 150px;
  width: 150px;
  height: 100px;
}

.div-1, .div-2 {
  position: absolute;
  top: 0;
  left: 0;
  width: 150px;
  height: 100px;
  border: 1px solid black;
  animation: rotate 30s linear infinite;
}
.div-1 {
  transform-origin: top left;
}
.div-2 {
  transform-origin: center center;
}

@keyframes rotate {
  100% {
    transform: rotate(360deg);
  }
}
<div class="wrapper">
  <div class="div-1"></div>
  <div class="div-2"></div>
</div>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文