因此,我写了一些代码将我的盒子从窗口上的某个位置平移到中心,并且我有原始的位置,例如:
原盒
{
top: 52.16538619995117,
right: 685.6694946289062,
bottom: 214.66795349121094,
left: 523.1669311523438,
width: 150
}
原始框的宽度/高度为150px,然后将其移动到中心时变为300px。一些基本代码通过以下方式将其重新定位到中心:
top = window.innerHeight / 2 - currentPosition.top - 150 /2
left = window.innerWidth / 2 - currentPosition.left - 150 /2
现在,假设该组件的新坐标为:
中心框
{
top: 74.78038787841797,
right: 928.7755126953125,
bottom: 374.7803955078125,
left: 628.7755126953125,
width: 300
}
我很麻烦用以下命令将其重新定位到中心:
transform: "translate(xTarget, yTarget)";
这应该比我想象的要容易得多,但是由于某些原因,它总是与原始版本不符。
这是我编写的帮助确定平移坐标的函数:
getCoordinateTarget(target, offset, currentNotePosition) {
const xDifference = target.left - currentNotePosition.left;
const yDifference = target.top - currentNotePosition.top;
const xTarget = xDifference > 0 ? xDifference - offset.x : -Math.abs(xDifference) - offset.x;
const yTarget = yDifference > 0 ? yDifference - offset.y : -Math.abs(yDifference) - offset.y;
return {
y: yTarget,
x: xTarget
};
}
最佳答案
老兄,我对CSS转换的工作方式有很大的误解。因此,我最初使用平移来移动它,我所要做的就是将转换设置为空,然后组件将重新定位到原始位置。
从字面上看transform: "";
关于javascript - CSS是否从中心翻译回来?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39336813/