【发布时间】:2021-11-14 13:28:44
【问题描述】:
因为我可以使用e.getBoundingClientRect().left从屏幕左侧获取元素的位置
当我们想要获取端点的位置时,根据需要添加宽度或高度,但是如何在元素旋转时获取端点的位置。
是否可以取值
let getBoxPos = document.querySelectorAll(".demo")
let box1 = document.querySelector("#box1")
let box2 = document.querySelector("#box2")
let box3 = document.querySelector("#box3")
let s = "";
function func() {
getBoxPos.forEach(e => {
let figXValue = e.getBoundingClientRect().left;
s = Number(s) + Number(1);
console.log("Start distance for box" + s + ":" + figXValue)
})
console.log("End distance for box1:" + (box1.getBoundingClientRect().left + 24));
console.log("End distance for box3:" + (box3.getBoundingClientRect().left + 104));
console.log("Don't know the correct way for box2:Is it box2.getBoundingClientRect().left + 100 or box2.getBoundingClientRect().left + 20 or none of them ");
}
func();
.demo {
height: 100px;
width: 20px;
margin-left: 40px;
border: 2px solid black;
}
.demo:nth-child(2) {
border: 2px solid red;
transform: rotate(-45deg);
}
.demo:nth-child(3) {
border: 2px solid green;
transform: rotate(-90deg);
}
<div class="demo" id="box1"></div>
<div class="demo" id="box2"></div>
<div class="demo" id="box3"></div>
【问题讨论】:
-
你知道它是围绕中心点旋转还是在其他地方?
-
@AHaworth 我使用了
transform-origin: top center;,所以它来自中心顶部,但值保持不变。如果您能提供任何不同或更好的解决方案,我将不胜感激
标签: javascript html css