@@ -5,20 +5,21 @@ export const animateScroll = (function () {
55 let resolvePrevious ;
66
77 return function animateScroll ( id , targetId , animate ) {
8- let targetElement = document . getElementById ( targetId ) ;
9- if ( ! targetElement ) {
10- targetElement = 'scrollTop' in document . documentElement
11- ? document . documentElement
12- : document . body ;
13- }
8+ const targetElement = document . getElementById ( targetId ) ;
149
1510 function getScrollTop ( ) {
1611 // like jQuery -> $('html, body').scrollTop
17- return targetElement . scrollTop ;
12+ return targetElement
13+ ? targetElement . scrollTop
14+ : document . documentElement . scrollTop || document . body . scrollTop ;
1815 }
1916
2017 function setScrollTop ( position ) {
21- targetElement . scrollTop = position ;
18+ if ( targetElement ) {
19+ targetElement . scrollTop = position ;
20+ } else {
21+ document . documentElement . scrollTop = document . body . scrollTop = position ;
22+ }
2223 }
2324
2425 return new Promise ( ( resolve , reject ) => {
@@ -29,7 +30,10 @@ export const animateScroll = (function () {
2930 }
3031
3132 function getOffsetTop ( ) {
32- return element . getBoundingClientRect ( ) . top - targetElement . getBoundingClientRect ( ) . top + getScrollTop ( ) ;
33+ const parentOffsetTop = targetElement
34+ ? targetElement . getBoundingClientRect ( ) . top
35+ : 0 ;
36+ return element . getBoundingClientRect ( ) . top - parentOffsetTop + getScrollTop ( ) ;
3337 }
3438
3539 const { offset, duration, easing } = animate ;
0 commit comments