2021-06-07 17:35:03 +08:00
|
|
|
function getScroll(w: Window) {
|
|
|
|
let ret = w.pageXOffset;
|
|
|
|
const method = '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
|
|
|
}
|
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
function getClientPosition(elem: HTMLElement) {
|
|
|
|
let x: number;
|
|
|
|
let y: number;
|
2019-01-12 11:33:27 +08:00
|
|
|
const doc = elem.ownerDocument;
|
2021-06-07 17:35:03 +08:00
|
|
|
const { body } = doc;
|
2019-01-12 11:33:27 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-07 17:35:03 +08:00
|
|
|
export function getOffsetLeft(el: HTMLElement) {
|
2019-01-12 11:33:27 +08:00
|
|
|
const pos = getClientPosition(el);
|
|
|
|
const doc = el.ownerDocument;
|
2021-06-07 17:35:03 +08:00
|
|
|
// Only IE use `parentWindow`
|
|
|
|
const w: Window = doc.defaultView || (doc as any).parentWindow;
|
2019-01-12 11:33:27 +08:00
|
|
|
pos.left += getScroll(w);
|
|
|
|
return pos.left;
|
2018-03-21 13:06:19 +08:00
|
|
|
}
|