@@ -24,7 +24,6 @@ class Sticky {
24
24
this . version = '1.1.9' ;
25
25
26
26
this . vp = this . getViewportSize ( ) ;
27
- this . scrollTop = this . getScrollTopPosition ( ) ;
28
27
this . body = document . querySelector ( 'body' ) ;
29
28
30
29
this . options = {
@@ -35,6 +34,12 @@ class Sticky {
35
34
stickyContainer : options . stickyContainer || 'body' ,
36
35
} ;
37
36
37
+ this . updateScrollTopPosition = this . updateScrollTopPosition . bind ( this ) ;
38
+
39
+ this . updateScrollTopPosition ( ) ;
40
+ window . addEventListener ( 'load' , this . updateScrollTopPosition ) ;
41
+ window . addEventListener ( 'scroll' , this . updateScrollTopPosition ) ;
42
+
38
43
this . run ( ) ;
39
44
}
40
45
@@ -215,8 +220,6 @@ class Sticky {
215
220
* @param {node } element - Element for which event function is fired
216
221
*/
217
222
onScrollEvents ( element ) {
218
- this . scrollTop = this . getScrollTopPosition ( ) ;
219
-
220
223
if ( element . sticky . active ) {
221
224
this . setPosition ( element ) ;
222
225
}
@@ -385,12 +388,12 @@ class Sticky {
385
388
386
389
387
390
/**
388
- * Function that returns scroll position offset from top
391
+ * Function that updates window scroll position
389
392
* @function
390
393
* @return {number }
391
394
*/
392
- getScrollTopPosition ( ) {
393
- return ( window . pageYOffset || document . scrollTop ) - ( document . clientTop || 0 ) || 0 ;
395
+ updateScrollTopPosition ( ) {
396
+ this . scrollTop = ( window . pageYOffset || document . scrollTop ) - ( document . clientTop || 0 ) || 0 ;
394
397
}
395
398
396
399
0 commit comments