2018-10-23 12:18:35 +08:00
|
|
|
export default function getScroll(target: HTMLElement | Window | null, top: boolean): number {
|
2016-09-29 18:00:23 +08:00
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prop = top ? 'pageYOffset' : 'pageXOffset';
|
|
|
|
const method = top ? 'scrollTop' : 'scrollLeft';
|
|
|
|
const isWindow = target === window;
|
|
|
|
|
2018-10-23 12:18:35 +08:00
|
|
|
let ret = isWindow ? (target as Window)[prop] : (target as HTMLElement)[method];
|
2016-09-29 18:00:23 +08:00
|
|
|
// ie6,7,8 standard mode
|
|
|
|
if (isWindow && typeof ret !== 'number') {
|
2018-10-23 12:18:35 +08:00
|
|
|
ret = (document.documentElement as HTMLElement)[method];
|
2016-09-29 18:00:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|