offset函数获取距离body的左,上偏移
/*
@params curElem 当前元素
@return Object[{left,top}]
@autor Tang World
*/
function offset(curElem){
let parent = curElem.offsetParent,
left = curElem.offsetLeft,
top = curElem.offsetTop;
while(parent && parent.tagName !== ‘BODY‘) {
left = parent.offsetLeft;
top = parent.offsetTop;
parent = parent.offsetParent;
}
return {
left,top
};
}
缺点:没有考虑IE8等浏览器的兼容
原文:https://www.cnblogs.com/rookie123/p/14412057.html