要获取元素相对于其父元素的顶部/左侧坐标,可以使用 offsetLeft 和 offsetTop 属性。返回值以像素(px)为单位。
const el = document.querySelector('.box');
console.log(el.offsetLeft, el.offsetTop);
Code language: JavaScript (javascript)
本教程对您有帮助吗?
要获取元素相对于其父元素的顶部/左侧坐标,可以使用 offsetLeft 和 offsetTop 属性。返回值以像素(px)为单位。
const el = document.querySelector('.box');
console.log(el.offsetLeft, el.offsetTop);
Code language: JavaScript (javascript)