javascript - 从 javascript 触发的 transformOrigin,旋转不正确

标签 javascript css

在 javascript 中使用 transformOrigin 时,旋转不正确。在 transform-origin 的设置中,我使用 X/Y 坐标 (100%/50%),因为 50% 是每个轴的中点。

问题: 运行动画时,动画的起点和终点都是正确的。在动画过程中,“线指示器”转动的 Angular 似乎不正确。如果您将动画想象成一个时钟,“线指示器”在旋转时显然会离开其中心。

通缉行为:我需要移动的“线指示器”在背景红色中心十字周围移动。我正在寻找一种使用 javascript transformOrigin 执行旋转的方法。

var deg = 180;

function myFunction() {
x = document.getElementById("indicator").style;
x.transform = "rotate(" + deg + "deg)";
x.transformOrigin = "100% 50%"; // Rotate in the center of div.
x.transition = "all 3s";
}
.indicator {
  z-index: 3;
  position: absolute;
  background-color: black;
  width: 150px;
  height: 10px;
  margin: 212px 0px 0px 100px;
}

.background-box {
  position: absolute;
  z-index: 1;
  background-color: white;
  height: 300px;
  width: 300px;
  margin: 50px 0px 0px 100px;
}

#button {
  height: 40px;
  width: 180px;
}


.checkmark-horizontal {
  z-index: 2;
  position: absolute;
  background-color: red;
  width: 300px;
  height: 3px;
  margin: 214px 0px 0px 100px;
}

.checkmark-vertical {
  z-index: 2;
  position: absolute;
  background-color: red;
  width: 3px;
  height: 300px;
  margin: 50px 0px 0px 250px;
}
<button id = "button" type = "button" name = "button" onclick = "myFunction()">Move indicator 180 degrees </button>

<div class="background-box"></div>

<div id="indicator" class="indicator"></div>

<div class="checkmark-horizontal"></div>
<div class="checkmark-vertical"></div>

最佳答案

在设置转换前需要设置transform-origin: 100% 50%;或者直接在css中使用

编辑

根据其他答案,最好不要为 transformOrigin 设置动画,而只为 transition 属性中的 transform 设置动画

var deg = 180;

function myFunction() {
  x = document.getElementById("indicator").style;
  x.transformOrigin = "100% 50%"; // Rotate in the center of div.
  x.transition = "transform 3s";
  x.transform = "rotate(" + deg + "deg)";



}
.indicator {
  z-index: 3;
  position: absolute;
  background-color: black;
  width: 150px;
  height: 10px;
  margin: 212px 0px 0px 100px;
}

.background-box {
  position: absolute;
  z-index: 1;
  background-color: white;
  height: 300px;
  width: 300px;
  margin: 50px 0px 0px 100px;
}

#button {
  height: 40px;
  width: 180px;
}

.checkmark-horizontal {
  z-index: 2;
  position: absolute;
  background-color: red;
  width: 300px;
  height: 3px;
  margin: 214px 0px 0px 100px;
}

.checkmark-vertical {
  z-index: 2;
  position: absolute;
  background-color: red;
  width: 3px;
  height: 300px;
  margin: 50px 0px 0px 250px;
}
<button id="button" type="button" name="button" onclick="myFunction()">Move indicator 180 degrees </button>

<div class="background-box"></div>

<div id="indicator" class="indicator"></div>

<div class="checkmark-horizontal"></div>
<div class="checkmark-vertical"></div>

关于javascript - 从 javascript 触发的 transformOrigin,旋转不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50742911/

相关文章:

javascript - 即使 Angular 中没有数据,也会显示分页

javascript - 添加和删​​除类 onClick 操作

javascript - 在 v2 上的 chart.js 中的图表上绘制水平线

javascript - 创建渐变/彩虹描边样式 html5 Canvas

css - Flex-box 被拉长

javascript - 将 css transition 应用于元素的宽度,使其覆盖其容器 div [ReactJS] 的 100% 宽度

javascript - 如何在javascript变量上添加换行符

javascript - 为什么 ngModel 不触发 writevalue 方法中的变化检测?

javascript - 查看页面时更改 CSS 代码

html - 使用伪类 :not and :before together