|
| 1 | +function getPageYOffset() { |
| 2 | + return window.pageYOffset; |
| 3 | +} |
| 4 | + |
| 5 | +function getBodyScrollTop () { |
| 6 | + return document.body.scrollTop; |
| 7 | +} |
| 8 | + |
| 9 | +function getDocumentElementScrollTop() { |
| 10 | + return document.documentElement.scrollTop; |
| 11 | +} |
| 12 | + |
| 13 | +function getScrollTopAuto() { |
| 14 | + if (typeof window === 'undefined') { |
| 15 | + return 0; |
| 16 | + } |
| 17 | + const pageYOffset = getPageYOffset(); |
| 18 | + if (pageYOffset) { |
| 19 | + // window.pageYOffset has a proper value, |
| 20 | + // thus future invoke use getPageYOffset(); |
| 21 | + _getScrollTop = getPageYOffset; |
| 22 | + return pageYOffset; |
| 23 | + } |
| 24 | + if (typeof document === 'undefined') { |
| 25 | + return 0; |
| 26 | + } |
| 27 | + if (document.body) { |
| 28 | + const bodyScrollTop = getBodyScrollTop(); |
| 29 | + if (bodyScrollTop) { |
| 30 | + // document.body.scrollTop has a proper value, |
| 31 | + // thus future invoke use getBodyScrollTop(); |
| 32 | + _getScrollTop = getBodyScrollTop; |
| 33 | + return bodyScrollTop; |
| 34 | + } |
| 35 | + } |
| 36 | + if (document.documentElement) { |
| 37 | + const documentElementScrollTop = getDocumentElementScrollTop(); |
| 38 | + if (documentElementScrollTop) { |
| 39 | + // document.documentElement.scrollTop has a proper value, |
| 40 | + // thus future invoke use getDocumentElementScrollTop(); |
| 41 | + _getScrollTop = getDocumentElementScrollTop; |
| 42 | + return documentElementScrollTop; |
| 43 | + } |
| 44 | + } |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +let _getScrollTop = getScrollTopAuto; |
| 49 | + |
| 50 | +const getScrollTop = () => { |
| 51 | + return _getScrollTop(); |
| 52 | +} |
| 53 | + |
| 54 | +export default getScrollTop; |
0 commit comments