11/**
2- * jQuery Scroll Direction Plugin 1.0.1
2+ * jQuery Scroll Direction Plugin 1.1.0
33 * https://github.com/phucbm/jquery-scroll-direction-plugin
44 *
55 * MIT License | Copyright (c) 2020 Minh-Phuc Bui
1010
1111 let obj = { } ,
1212 pluginActive = false ,
13+ $w = $ ( window ) ,
1314 settings = {
14- topOffset : 0 ,
15- bottomOffset : 0 ,
15+ topOffset : ( ) => 0 ,
16+ bottomOffset : ( ) => 0 ,
1617 atBottomIsAtMiddle : true ,
1718 indicator : true ,
1819 indicatorElement : $ ( "body" ) ,
2122 scrollAtTopClass : "scroll-top" ,
2223 scrollAtMiddleClass : "scroll-middle" ,
2324 scrollAtBottomClass : "scroll-bottom" ,
24- extraIndicators : { }
25- } ;
25+ extraIndicators : { } ,
26+ scrollAmount : ( ) => $w . scrollTop ( ) ,
27+ maxScrollAmount : ( ) => $ ( document ) . height ( ) - $w . height ( ) ,
28+ hijacking : false , // turn this on to run update() in custom event
29+ } ,
30+ lastScrollAmount = false ;
2631
2732 // Method: init()
2833 obj . init = function ( options ) {
2934 pluginActive = true ;
3035 settings = $ . extend ( settings , options ) ;
36+ console . log ( settings ) ;
37+ } ;
38+
39+ // Method: update() for custom hijacking event
40+ obj . update = function ( options ) {
41+ settings = $ . extend ( settings , options ) ;
42+
43+ // on hijacking
44+ if ( settings . hijacking ) {
45+ update ( ) ;
46+ }
3147 } ;
3248
3349 // APIs
7793 }
7894 }
7995
80- // Main process
81- let $w = $ ( window ) ,
82- lastScrollAmount = false ,
83- scrollAmount = $w . scrollTop ( ) ,
84- maxScrollAmount = $ ( document ) . height ( ) - $w . height ( ) ;
85- $w . on ( "load scroll resize" , function ( ) {
96+ // update
97+ function update ( ) {
8698 if ( pluginActive ) {
87- // update values
88- scrollAmount = $w . scrollTop ( ) ;
89- maxScrollAmount = $ ( document ) . height ( ) - $w . height ( ) ;
90-
9199 // check scroll directions
92- if ( scrollAmount > lastScrollAmount && lastScrollAmount >= 0 ) {
100+ if ( settings . scrollAmount ( ) > lastScrollAmount && lastScrollAmount >= 0 ) {
93101 // scroll down
94102 obj . isScrollUp = false ;
95103 obj . isScrollDown = true ;
96104
97105 $w . trigger ( scrollDown ) ;
98- } else if ( scrollAmount < lastScrollAmount && lastScrollAmount >= 0 ) {
106+ } else if ( settings . scrollAmount ( ) < lastScrollAmount && lastScrollAmount >= 0 ) {
99107 // scroll up
100108 obj . isScrollUp = true ;
101109 obj . isScrollDown = false ;
102110
103111 $w . trigger ( scrollUp ) ;
104- } else if ( scrollAmount < 0 ) {
112+ } else if ( settings . scrollAmount ( ) < 0 ) {
105113 // scroll up (elastic scroll with negative value)
106114 obj . isScrollUp = true ;
107115 obj . isScrollDown = false ;
108116
109117 $w . trigger ( scrollUp ) ;
110- } else if ( scrollAmount > maxScrollAmount ) {
118+ } else if ( settings . scrollAmount ( ) > settings . maxScrollAmount ( ) ) {
111119 // scroll down (elastic scroll with positive value)
112120 obj . isScrollUp = false ;
113121 obj . isScrollDown = true ;
116124 }
117125
118126 // update the last position
119- lastScrollAmount = scrollAmount ;
127+ lastScrollAmount = settings . scrollAmount ( ) ;
120128
121129 // check scroll positions
122- if ( scrollAmount <= settings . topOffset ) {
130+ if ( settings . scrollAmount ( ) <= settings . topOffset ( ) ) {
123131 // at top
124132 obj . isScrollAtTop = true ;
125133 obj . isScrollAtMiddle = false ;
126134 obj . isScrollAtBottom = false ;
127135
128136 $w . trigger ( scrollAtTop ) ;
129137 } else if (
130- scrollAmount >= maxScrollAmount - settings . bottomOffset &&
131- scrollAmount <= maxScrollAmount
138+ settings . scrollAmount ( ) >= settings . maxScrollAmount ( ) - settings . bottomOffset ( ) &&
139+ settings . scrollAmount ( ) <= settings . maxScrollAmount ( )
132140 ) {
133141 // at bottom
134142 obj . isScrollAtTop = false ;
158166 l = settings . extraIndicators . length ;
159167 for ( i ; i < l ; i ++ ) {
160168 indicator ( {
161- "values" : [ scrollAmount >= settings . extraIndicators [ i ] [ "element" ] . offset ( ) . top ] ,
169+ "values" : [ settings . scrollAmount ( ) >= settings . extraIndicators [ i ] [ "element" ] . offset ( ) . top ] ,
162170 "classes" : [ settings . extraIndicators [ i ] [ "class" ] ]
163171 } ) ;
164172 }
165173
166174 $w . trigger ( scrollDirection ) ;
167175 }
168- } ) ;
176+ }
177+
178+ // update on window events
179+ if ( ! settings . hijacking ) {
180+ $w . on ( "load scroll resize" , function ( ) {
181+ // update values
182+ update ( ) ;
183+ } ) ;
184+ }
169185
170186 // Only assign to jQuery.scrollDirection if jQuery is loaded
171187 if ( jQuery ) {
172188 jQuery . scrollDirection = obj ;
173189 }
174- } ) ( jQuery ) ;
190+ } ) ( jQuery ) ;
0 commit comments