Skip to content

Commit ebd0275

Browse files
committed
add "Acceleration" event.
1 parent 5a10568 commit ebd0275

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

dist/js-scroll-effect-module.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js-scroll-effect-module.js

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@ export default class SCROLL_EFFECT_MODULE {
2929
firstElemDelayTime : options.firstElemDelayTime||0,
3030
firstDelayTime : options.firstDelayTime||300,
3131
loadDelayTime : options.loadDelayTime||0,
32-
addClassNameActive : options.addClassNameActive||'is-active'
32+
addClassNameActive : options.addClassNameActive||'is-active',
33+
acceleration : options.acceleration||false,
3334
}
3435

36+
this.NumScrolltopPre = window.pageYOffset;
37+
this.NumScrolltop = this.NumScrolltopPre;
38+
this.NumAcceleration = 0;
39+
3540
// Set callback functions.
3641
if(!options.on){
3742
options.on = {}
3843
}
3944
this.on = {
40-
In : options.on.In||'',
41-
Out : options.on.Out||''
45+
In : options.on.In||'',
46+
Out : options.on.Out||'',
47+
Acceleration : options.on.Acceleration||''
4248
}
4349

4450
// Store element information.
@@ -132,11 +138,50 @@ export default class SCROLL_EFFECT_MODULE {
132138
}
133139
});
134140

141+
if(this.Config.acceleration){
142+
if(Math.abs(this.NumAcceleration) <= Math.abs(this.NumScrolltop - this.NumScrolltopPre)){
143+
this.NumAcceleration = this.NumScrolltop - this.NumScrolltopPre;
144+
145+
if(this.NumAcceleration >= 100) this.NumAcceleration = 100;
146+
if(this.NumAcceleration <= -100) this.NumAcceleration = -100;
147+
148+
clearInterval(this.Interval);
149+
this.CheckAcceleration();
150+
}
151+
152+
// Callback function.
153+
if(this.on.Acceleration && typeof(this.on.Acceleration) === 'function') this.on.Acceleration(this.NumAcceleration);
154+
}
155+
135156
if(method === 'load'){
136157
this.ActionAddClassFirst();
137158
} else if(method === 'scroll'){
138159
this.ActionAddClass();
139160
}
161+
162+
this.NumScrolltopPre = this.NumScrolltop;
163+
}
164+
165+
// For Config.acceleration == true.
166+
CheckAcceleration(){
167+
this.Interval = setInterval(()=>{
168+
169+
let _racio = Math.pow(1.02, Math.abs(this.NumAcceleration)) - 0.6;
170+
if(this.NumAcceleration > 0){
171+
this.NumAcceleration = this.NumAcceleration - _racio;
172+
} else if(this.NumAcceleration < 0){
173+
this.NumAcceleration = this.NumAcceleration + _racio;
174+
}
175+
this.NumAcceleration = Math.ceil(this.NumAcceleration * 100) / 100;
176+
177+
if(this.NumAcceleration > -0.8 && this.NumAcceleration < 0.8){
178+
this.NumAcceleration = 0;
179+
clearInterval(this.Interval);
180+
}
181+
182+
// Callback function.
183+
if(this.on.Acceleration && typeof(this.on.Acceleration) === 'function') this.on.Acceleration(this.NumAcceleration);
184+
},10);
140185
}
141186

142187
ActionAddClassFirst(){
@@ -177,7 +222,7 @@ export default class SCROLL_EFFECT_MODULE {
177222
dom.addClass(this.$elemItem[el], this.Config.addClassNameActive);
178223

179224
// Callback function.
180-
if(this.on.In && typeof(this.on.In) === 'function') this.on.In(this.$elemItem[el], el);
225+
if(this.on.In && typeof(this.on.In) === 'function') this.on.In(this.$elemItem[el], el, this.NumScrolltop);
181226
}
182227
});
183228

@@ -187,7 +232,7 @@ export default class SCROLL_EFFECT_MODULE {
187232
dom.removeClass(this.$elemItem[el], this.Config.addClassNameActive);
188233

189234
// Callback function.
190-
if(this.on.Out && typeof(this.on.Out) === 'function') this.on.Out(this.$elemItem[el], el);
235+
if(this.on.Out && typeof(this.on.Out) === 'function') this.on.Out(this.$elemItem[el], el, this.NumScrolltop);
191236
}
192237
});
193238
}

0 commit comments

Comments
 (0)