📅  最后修改于: 2022-03-11 15:01:14.848000             🧑  作者: Mango
function offset(el) {
const rect = el.getBoundingClientRect(),
scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
scrollTop = window.pageYOffset || document.documentElement.scrollTop;
return { top: rect.top + scrollTop, left: rect.left + scrollLeft }
}
// example use
const div = document.querySelector('div');
const divOffset = offset(div);
console.log(divOffset.left, divOffset.top);