forked from chrisgannon/ScrollLottie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrollLottie.js
35 lines (32 loc) · 954 Bytes
/
ScrollLottie.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
const ScrollLottie = (obj) => {
let anim = lottie.loadAnimation({
container: document.querySelector(obj.target),
renderer: 'svg',
loop: false,
autoplay: false,
path: obj.path
});
let timeObj = {currentFrame: 0}
let endString = (obj.speed === "slow") ? "+=2000" : (obj.speed === "medium") ? "+=1000" : (obj.speed === undefined) ? "+=1250" : "+=500";
ScrollTrigger.create({
trigger: obj.target,
scrub: true,
pin: true,
start: "top top",
end: endString,
onUpdate: self => {
if(obj.duration) {
gsap.to(timeObj, {
duration: obj.duration,
currentFrame:(Math.floor(self.progress * (anim.totalFrames - 1))),
onUpdate: () => {
anim.goToAndStop(timeObj.currentFrame, true)
},
ease: 'expo'
})
} else {
anim.goToAndStop(self.progress * (anim.totalFrames - 1), true)
}
}
});
}