-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
executable file
·85 lines (71 loc) · 1.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import gsap from "gsap";
export default class GScroll
{
constructor (elmt, speed, onUpdate = () => true)
{
this.speed = speed/10 || 0.06;
this.elmt = elmt;
this.isWheeling = null;
this.deltaY = 0;
this.update = onUpdate;
}
init ()
{
this.current = this.scrollTop = 0;
this.height = document.querySelector(this.elmt).clientHeight-window.innerHeight;
this.deplacement = gsap.quickSetter(this.elmt, "y", "px");
this.addTicker = () => {
this.playTicker();
}
gsap.ticker.add(this.addTicker);
}
wheel()
{
window.addEventListener('wheel', this.ref = (e) => {
this.deltaY = e.deltaY;
window.clearTimeout( this.isWheeling );
this.isWheeling = setTimeout( (e) => {
this.deltaY = 0;
}, 66);
});
}
unwheel()
{
window.removeEventListener('wheel', this.ref);
}
resize()
{
this.height = document.querySelector(this.elmt).clientHeight-window.innerHeight;
}
scrollTo(section, dur)
{
const duration = dur || 1;
gsap.to(this, {
scrollTop: document.querySelector(section).getBoundingClientRect().top - this.current,
duration,
ease:'power3.inOut'
})
}
playTicker(){
const dt = 1.0 - Math.pow(1.0 - this.speed, gsap.ticker.deltaRatio());
if(this.scrollTop + this.deltaY > this.height){
this.scrollTop = this.height;
}else if(this.scrollTop + this.deltaY < 0){
this.scrollTop = 0;
}else if(this.deltaY !== 0){
this.scrollTop += this.deltaY;
}
const diff = -this.scrollTop - this.current;
if(Math.round(100*diff)/100 != 0){
this.current += diff * dt;
this.deplacement(this.current);
}
this.update();
}
destroy()
{
gsap.killTweensOf(this.elmt);
window.removeEventListener('wheel', this.ref);
gsap.ticker.remove(this.addTicker);
}
}