元素相对浏览器顶部位置
作者:
清霆 | 来源:发表于
2021-11-05 16:33 被阅读0次export const formatOffset = (el) => {
let offsetTop = 0;
do {
if (!isNaN(el.offsetTop)) {
offsetTop += el.offsetTop;
}
} while ((el = el.offsetParent));
return { top: offsetTop };
}
export const formatOffsetRelative = (el) => {
if (el.getBoundingClientRect) {
return el.getBoundingClientRect();
}
else {
var x = 0, y = 0;
do {
x += el.offsetLeft - el.scrollLeft;
y += el.offsetTop - el.scrollTop;
}
while (el = el.offsetParent);
return { top: y, left: x }
}
}
引自 :
get-element-relative-offset
offset
本文标题:元素相对浏览器顶部位置
本文链接:https://www.haomeiwen.com/subject/hvurzltx.html
网友评论