66 * Date: 2021-06-20
77 */
88
9- ( function ( $ ) {
9+ ( function ( $ ) {
1010 "use strict" ;
1111
1212 let obj = { } ,
2424 scrollAtMiddleClass : "scroll-middle" ,
2525 scrollAtBottomClass : "scroll-bottom" ,
2626 extraIndicators : { } ,
27- scrollAmount : ( ) => $w . scrollTop ( ) ,
28- maxScrollAmount : ( ) => $ ( document ) . height ( ) - $w . height ( ) ,
27+ scrollAmount : ( ) => ( window . pageYOffset || document . documentElement . scrollTop ) - ( document . documentElement . clientTop || 0 ) ,
28+ maxScrollAmount : ( ) => $ ( document ) . height ( ) - ( window . innerHeight || document . documentElement . clientHeight ) ,
2929 hijacking : false , // turn this on to run update() in custom event
3030 } ,
3131 lastScrollAmount = false ;
3232
3333 // Method: init()
34- obj . init = function ( options ) {
34+ obj . init = function ( options ) {
3535 pluginActive = true ;
36- settings = $ . extend ( settings , options ) ;
36+ settings = { ... settings , ... options } ;
3737 } ;
3838
3939 // Method: update() for custom hijacking event
40- obj . update = function ( options ) {
41- settings = $ . extend ( settings , options ) ;
40+ obj . update = function ( options ) {
41+ settings = { ... settings , ... options } ;
4242
4343 // on hijacking
44- if ( settings . hijacking ) {
44+ if ( settings . hijacking ) {
4545 update ( ) ;
4646 }
4747 } ;
6262 scrollAtBottom = $ . Event ( "scrollAtBottom" ) ;
6363
6464 // Indicator: add class to indicate the scrolling status
65- function indicator ( options ) {
66- if ( settings . indicator ) {
67- let indicators = $ . extend ( {
68- "values" : [
69- obj . isScrollUp ,
70- obj . isScrollDown ,
71- obj . isScrollAtTop ,
72- obj . isScrollAtMiddle ,
73- obj . isScrollAtBottom
74- ] ,
75- "classes" : [
76- settings . scrollUpClass ,
77- settings . scrollDownClass ,
78- settings . scrollAtTopClass ,
79- settings . scrollAtMiddleClass ,
80- settings . scrollAtBottomClass
81- ]
82- } , options ) ,
65+ function indicator ( options ) {
66+ if ( settings . indicator ) {
67+ let indicators = {
68+ ...{
69+ "values" : [
70+ obj . isScrollUp ,
71+ obj . isScrollDown ,
72+ obj . isScrollAtTop ,
73+ obj . isScrollAtMiddle ,
74+ obj . isScrollAtBottom
75+ ] ,
76+ "classes" : [
77+ settings . scrollUpClass ,
78+ settings . scrollDownClass ,
79+ settings . scrollAtTopClass ,
80+ settings . scrollAtMiddleClass ,
81+ settings . scrollAtBottomClass
82+ ]
83+ } , ...options
84+ } ,
8385 i = 0 ,
8486 l = indicators . values . length ;
8587
86- for ( i ; i < l ; i ++ ) {
87- if ( indicators . values [ i ] ) {
88+ for ( i ; i < l ; i ++ ) {
89+ if ( indicators . values [ i ] ) {
8890 settings . indicatorElement . addClass ( indicators . classes [ i ] ) ;
89- } else {
91+ } else {
9092 settings . indicatorElement . removeClass ( indicators . classes [ i ] ) ;
9193 }
9294 }
9395 }
9496 }
9597
9698 // update
97- function update ( ) {
98- if ( pluginActive ) {
99+ function update ( ) {
100+ if ( pluginActive ) {
99101 // check scroll directions
100- if ( settings . scrollAmount ( ) > lastScrollAmount && lastScrollAmount >= 0 ) {
102+ if ( settings . scrollAmount ( ) > lastScrollAmount && lastScrollAmount >= 0 ) {
101103 // scroll down
102104 obj . isScrollUp = false ;
103105 obj . isScrollDown = true ;
104106
105107 $w . trigger ( scrollDown ) ;
106- } else if ( settings . scrollAmount ( ) < lastScrollAmount && lastScrollAmount >= 0 ) {
108+ } else if ( settings . scrollAmount ( ) < lastScrollAmount && lastScrollAmount >= 0 ) {
107109 // scroll up
108110 obj . isScrollUp = true ;
109111 obj . isScrollDown = false ;
110112
111113 $w . trigger ( scrollUp ) ;
112- } else if ( settings . scrollAmount ( ) < 0 ) {
114+ } else if ( settings . scrollAmount ( ) < 0 ) {
113115 // scroll up (elastic scroll with negative value)
114116 obj . isScrollUp = true ;
115117 obj . isScrollDown = false ;
116118
117119 $w . trigger ( scrollUp ) ;
118- } else if ( settings . scrollAmount ( ) > settings . maxScrollAmount ( ) ) {
120+ } else if ( settings . scrollAmount ( ) > settings . maxScrollAmount ( ) ) {
119121 // scroll down (elastic scroll with positive value)
120122 obj . isScrollUp = false ;
121123 obj . isScrollDown = true ;
127129 lastScrollAmount = settings . scrollAmount ( ) ;
128130
129131 // check scroll positions
130- if ( settings . scrollAmount ( ) <= settings . topOffset ( ) ) {
132+ if ( settings . scrollAmount ( ) <= settings . topOffset ( ) ) {
131133 // at top
132134 obj . isScrollAtTop = true ;
133135 obj . isScrollAtMiddle = false ;
134136 obj . isScrollAtBottom = false ;
135137
136138 $w . trigger ( scrollAtTop ) ;
137- } else if (
139+ } else if (
138140 settings . scrollAmount ( ) >= settings . maxScrollAmount ( ) - settings . bottomOffset ( ) &&
139141 settings . scrollAmount ( ) <= settings . maxScrollAmount ( )
140- ) {
142+ ) {
141143 // at bottom
142144 obj . isScrollAtTop = false ;
143145 obj . isScrollAtMiddle = false ;
144146 obj . isScrollAtBottom = true ;
145147
146148 $w . trigger ( scrollAtBottom ) ;
147149
148- if ( settings . atBottomIsAtMiddle ) {
150+ if ( settings . atBottomIsAtMiddle ) {
149151 obj . isScrollAtMiddle = true ;
150152 $w . trigger ( scrollAtMiddle ) ;
151153 }
152- } else {
154+ } else {
153155 // at middle
154156 obj . isScrollAtTop = false ;
155157 obj . isScrollAtMiddle = true ;
164166 // Extra indicators
165167 let i = 0 ,
166168 l = settings . extraIndicators . length ;
167- for ( i ; i < l ; i ++ ) {
169+ for ( i ; i < l ; i ++ ) {
168170 indicator ( {
169171 "values" : [ settings . scrollAmount ( ) >= settings . extraIndicators [ i ] [ "element" ] . offset ( ) . top ] ,
170172 "classes" : [ settings . extraIndicators [ i ] [ "class" ] ]
176178 }
177179
178180 // update on window events
179- if ( ! settings . hijacking ) {
180- $w . on ( "load scroll resize" , function ( ) {
181+ if ( ! settings . hijacking ) {
182+ $w . on ( "load scroll resize" , function ( ) {
181183 // update values
182184 update ( ) ;
183185 } ) ;
184186 }
185187
186188 // Only assign to jQuery.scrollDirection if jQuery is loaded
187- if ( jQuery ) {
189+ if ( jQuery ) {
188190 jQuery . scrollDirection = obj ;
189191 }
190192} ) ( jQuery ) ;
0 commit comments