2019-01-12 11:33:27 +08:00
|
|
|
function getScroll(w, top) {
|
|
|
|
let ret = top ? w.pageYOffset : w.pageXOffset;
|
|
|
|
const method = top ? 'scrollTop' : 'scrollLeft';
|
2018-03-21 13:06:19 +08:00
|
|
|
if (typeof ret !== 'number') {
|
2019-01-12 11:33:27 +08:00
|
|
|
const d = w.document;
|
2018-03-21 13:06:19 +08:00
|
|
|
// ie6,7,8 standard mode
|
2019-01-12 11:33:27 +08:00
|
|
|
ret = d.documentElement[method];
|
2018-03-21 13:06:19 +08:00
|
|
|
if (typeof ret !== 'number') {
|
|
|
|
// quirks mode
|
2019-01-12 11:33:27 +08:00
|
|
|
ret = d.body[method];
|
2018-03-21 13:06:19 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
return ret;
|
2018-03-21 13:06:19 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function getClientPosition(elem) {
|
|
|
|
let x;
|
|
|
|
let y;
|
|
|
|
const doc = elem.ownerDocument;
|
|
|
|
const body = doc.body;
|
|
|
|
const docElem = doc && doc.documentElement;
|
|
|
|
const box = elem.getBoundingClientRect();
|
|
|
|
x = box.left;
|
|
|
|
y = box.top;
|
|
|
|
x -= docElem.clientLeft || body.clientLeft || 0;
|
|
|
|
y -= docElem.clientTop || body.clientTop || 0;
|
2018-03-21 13:06:19 +08:00
|
|
|
return {
|
|
|
|
left: x,
|
|
|
|
top: y,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-21 13:06:19 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export function getOffsetLeft(el) {
|
|
|
|
const pos = getClientPosition(el);
|
|
|
|
const doc = el.ownerDocument;
|
|
|
|
const w = doc.defaultView || doc.parentWindow;
|
|
|
|
pos.left += getScroll(w);
|
|
|
|
return pos.left;
|
2018-03-21 13:06:19 +08:00
|
|
|
}
|