3 回答
TA贡献1863条经验 获得超2个赞
像素中第一个和第二个元素之间的距离:
window.addEventListener('load', () => {
console.log(document.querySelectorAll('.containt')[1].getBoundingClientRect().left - document.querySelectorAll('.containt')[0].clientWidth);
})
.container {
width: 400px;
height: 200px;
background-color: orange;
display: flex;
justify-content: space-between;
}
.containt {
width: 50px;
height: 50px;
background-color: blue;
}
<div class=container>
<div class="containt">
</div>
<div class="containt">
</div>
<div class="containt">
</div>
</div>
TA贡献1752条经验 获得超4个赞
在这里我找到了如何测量元素 之间的距离测量两个 HTML 元素中心之间的距离
function getPositionAtCenter(element) {
const {top, left, width, height} = element.getBoundingClientRect();
return {
x: left + width / 2,
y: top + height / 2
};
}
function getDistanceBetweenElements(a, b) {
const aPosition = getPositionAtCenter(a);
const bPosition = getPositionAtCenter(b);
return Math.hypot(aPosition.x - bPosition.x, aPosition.y - bPosition.y);
}
const distance = getDistanceBetweenElements(
document.getElementById("x"),
document.getElementById("y")
);
console.log(distance)
<div id="x"></div>
<div id="y" style="margin-left:100px;"></div>
TA贡献1784条经验 获得超7个赞
$(document).ready(()=>{
let current_item_children = $("div.sec").find('img');
let children = [];
$.each(current_item_children, function () {
var this_item = $(this);
children.push(
{
width: this_item.width(),
outerWidth:this_item.outerWidth(false),
height: this_item.height(),
}
);
console.log(children);
});
});
试试这个功能
添加回答
举报