Skip to content

Commit 3870984

Browse files
committed
adjust intersect function
1 parent 667d707 commit 3870984

File tree

1 file changed

+99
-41
lines changed

1 file changed

+99
-41
lines changed

src/js-scroll-effect-module.js

Lines changed: 99 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ export default class SCROLL_EFFECT_MODULE {
1212
this.state = {
1313
NumScrolltopPre: window.pageYOffset,
1414
NumScrolltop : window.pageYOffset,
15-
NumAcceleration: 0,
1615
PosList : [],
1716
PosListFixPre : [],
1817
PosListFix : [],
19-
PosListNoneFix : []
18+
PosListNoneFix : [],
19+
flg: {
20+
intersectChanged: false,
21+
}
2022
};
2123

2224
// config, options.
@@ -37,14 +39,16 @@ export default class SCROLL_EFFECT_MODULE {
3739

3840
autoStart : true,
3941

40-
acceleration : false,
42+
intersect : false,
43+
addClassNameIntersect : 'is-intersect',
44+
addClassNameIntersectOver : 'is-intersect-over',
4145

4246
on: {
4347
Scroll : null,
4448
Change : null,
4549
In : null,
4650
Out : null,
47-
Acceleration : null
51+
Intersect : null,
4852
}
4953
};
5054
// Merge Config Settings.
@@ -123,10 +127,14 @@ export default class SCROLL_EFFECT_MODULE {
123127
if(_elem){
124128
_elem.map((el,i)=>{
125129
let obj = {
130+
el: el,
126131
index: i,
127132
pos: el.getBoundingClientRect().top + this.state.NumScrolltop,
128133
count: 0,
129-
active: false
134+
active: false,
135+
intersect: false,
136+
intersectover: false,
137+
dataset: el.dataset
130138
};
131139
this.state.PosList.push( obj );
132140
});
@@ -213,60 +221,52 @@ export default class SCROLL_EFFECT_MODULE {
213221
this.state.PosListNoneFix.push(el);
214222
}
215223
}
216-
217224
}
218-
});
219-
220-
if(this.config.acceleration){
221-
if(Math.abs(this.state.NumAcceleration) <= Math.abs(this.state.NumScrolltop - this.state.NumScrolltopPre)){
222-
this.state.NumAcceleration = this.state.NumScrolltop - this.state.NumScrolltopPre;
223225

224-
if(this.state.NumAcceleration >= 100) this.state.NumAcceleration = 100;
225-
if(this.state.NumAcceleration <= -100) this.state.NumAcceleration = -100;
226-
227-
clearInterval(this.Interval);
228-
this.CheckAcceleration();
226+
if(this.config.intersect){
227+
if( this.state.NumScrolltop > el.pos ){
228+
if( this.state.NumScrolltop + this.NumWindowHeight > el.pos + el.height ){
229+
if(el.intersect){
230+
this.state.flg.intersectChanged = true;
231+
el.intersect = false;
232+
el.intersectover = true;
233+
}
234+
} else {
235+
if(!el.intersect){
236+
this.state.flg.intersectChanged = true;
237+
el.intersect = true;
238+
el.intersectover = false;
239+
}
240+
}
241+
} else {
242+
if(el.intersect){
243+
this.state.flg.intersectChanged = true;
244+
el.intersect = false;
245+
el.intersectover = false;
229246
}
230-
231-
// Callback function.
232-
if(this.config.on.Acceleration && typeof(this.config.on.Acceleration) === 'function') this.config.on.Acceleration(this.state.NumAcceleration);
233247
}
248+
}
249+
250+
});
234251

235252
if(method === 'load'){
236253
this.ActionChangeFirst();
237254
} else if(method === 'scroll'){
238255
if(this.state.PosListFixPre.length !== this.state.PosListFix.length) this._actionChange();
239256
}
240257

258+
if(this.config.intersect && this.state.flg.intersectChanged){
259+
this.state.flg.intersectChanged = false;
260+
this._actionChangeIntersect();
261+
}
262+
241263
// Callback function.
242264
if(this.config.on.Scroll && typeof(this.config.on.Scroll) === 'function') this.config.on.Scroll(this.state.NumScrolltop);
243265

244266
this.state.NumScrolltopPre = this.state.NumScrolltop;
245267
this.state.PosListFixPre = this.state.PosListFix;
246268
}
247269

248-
// For Config.acceleration == true.
249-
CheckAcceleration(){
250-
this.Interval = setInterval(()=>{
251-
252-
let _racio = Math.pow(1.02, Math.abs(this.state.NumAcceleration)) - 0.6;
253-
if(this.state.NumAcceleration > 0){
254-
this.state.NumAcceleration = this.state.NumAcceleration - _racio;
255-
} else if(this.state.NumAcceleration < 0){
256-
this.state.NumAcceleration = this.state.NumAcceleration + _racio;
257-
}
258-
this.state.NumAcceleration = Math.ceil(this.state.NumAcceleration * 100) / 100;
259-
260-
if(this.state.NumAcceleration > -0.8 && this.state.NumAcceleration < 0.8){
261-
this.state.NumAcceleration = 0;
262-
clearInterval(this.Interval);
263-
}
264-
265-
// Callback function.
266-
if(this.config.on.Acceleration && typeof(this.config.on.Acceleration) === 'function') this.config.on.Acceleration(this.state.NumAcceleration);
267-
},10);
268-
}
269-
270270
ActionChangeFirst(){
271271
let loopCount = 0;
272272

@@ -334,4 +334,62 @@ export default class SCROLL_EFFECT_MODULE {
334334

335335
}
336336

337+
_actionChangeIntersect(){
338+
339+
this.state.PosList.map((el)=>{
340+
341+
let callback = {
342+
el: el.el,
343+
intersect: el.intersect,
344+
index: el.index,
345+
pos: el.pos,
346+
height: el.height,
347+
height2: el.height2,
348+
count: el.count,
349+
dataset: el.dataset
350+
};
351+
352+
if(el.intersect){
353+
if(!DOM.hasClass(el.el, this.config.addClassNameIntersect)){
354+
if(this.config.addClassNameIntersect) DOM.addClass(el.el, this.config.addClassNameIntersect);
355+
356+
// Callback function.
357+
if(this.config.on.Intersect && typeof(this.config.on.Intersect) === 'function'){
358+
this.config.on.Intersect(callback);
359+
}
360+
}
361+
} else {
362+
if(DOM.hasClass(el.el, this.config.addClassNameIntersect)){
363+
if(this.config.addClassNameIntersect) DOM.removeClass(el.el, this.config.addClassNameIntersect);
364+
365+
// Callback function.
366+
if(this.config.on.Intersect && typeof(this.config.on.Intersect) === 'function'){
367+
this.config.on.Intersect(callback);
368+
}
369+
}
370+
}
371+
372+
if(el.intersectover){
373+
if(!DOM.hasClass(el.el, this.config.addClassNameIntersectOver)){
374+
if(this.config.addClassNameIntersectOver) DOM.addClass(el.el, this.config.addClassNameIntersectOver);
375+
376+
// Callback function.
377+
if(this.config.on.Intersect && typeof(this.config.on.Intersect) === 'function'){
378+
this.config.on.Intersect(callback);
379+
}
380+
}
381+
} else {
382+
if(DOM.hasClass(el.el, this.config.addClassNameIntersectOver)){
383+
if(this.config.addClassNameIntersectOver) DOM.removeClass(el.el, this.config.addClassNameIntersectOver);
384+
385+
// Callback function.
386+
if(this.config.on.Intersect && typeof(this.config.on.Intersect) === 'function'){
387+
this.config.on.Intersect(callback);
388+
}
389+
}
390+
}
391+
});
392+
393+
}
394+
337395
}

0 commit comments

Comments
 (0)