| 1 |
/** |
| 2 |
* Block Lottie |
| 3 |
*/ |
| 4 |
const { |
| 5 |
GHOSTKIT: { events }, |
| 6 |
Motion: { inView, scroll }, |
| 7 |
} = window; |
| 8 |
|
| 9 |
// Click trigger. |
| 10 |
events.on( |
| 11 |
document, |
| 12 |
'click', |
| 13 |
'.ghostkit-lottie[data-trigger="click"] > lottie-player', |
| 14 |
(e) => { |
| 15 |
if (e.delegateTarget?.play) { |
| 16 |
e.delegateTarget.play(); |
| 17 |
} |
| 18 |
} |
| 19 |
); |
| 20 |
|
| 21 |
// In Viewport trigger. |
| 22 |
inView('.ghostkit-lottie[data-trigger="viewport"] > lottie-player', (info) => { |
| 23 |
if (info?.target?.play) { |
| 24 |
info.target.play(); |
| 25 |
} |
| 26 |
|
| 27 |
return (leaveInfo) => { |
| 28 |
if (leaveInfo?.target?.pause) { |
| 29 |
leaveInfo.target.pause(); |
| 30 |
} |
| 31 |
}; |
| 32 |
}); |
| 33 |
|
| 34 |
// Scroll trigger. |
| 35 |
events.on(document, 'init.blocks.gkt', () => { |
| 36 |
document |
| 37 |
.querySelectorAll( |
| 38 |
'.ghostkit-lottie[data-trigger="scroll"]:not(.ghostkit-lottie-ready)' |
| 39 |
) |
| 40 |
.forEach(($this) => { |
| 41 |
const lottieEl = $this.querySelector('lottie-player'); |
| 42 |
const reverse = lottieEl.getAttribute('direction') === '-1'; |
| 43 |
|
| 44 |
$this.classList.add('ghostkit-lottie-ready'); |
| 45 |
|
| 46 |
scroll( |
| 47 |
({ y }) => { |
| 48 |
if (lottieEl?._lottie) { |
| 49 |
const { progress } = y; |
| 50 |
const { totalFrames, goToAndStop } = lottieEl._lottie; |
| 51 |
let newFrame = Math.round(progress * totalFrames); |
| 52 |
|
| 53 |
if (reverse) { |
| 54 |
newFrame = totalFrames - newFrame; |
| 55 |
} |
| 56 |
|
| 57 |
// Check if the current frame is the last frame. If it's the last frame, do nothing. This prevents showing the empty frame at the end. Thanks Pauline for pointing out. |
| 58 |
if (newFrame < totalFrames) { |
| 59 |
goToAndStop.call(lottieEl._lottie, newFrame, true); |
| 60 |
} |
| 61 |
} |
| 62 |
}, |
| 63 |
{ |
| 64 |
target: lottieEl, |
| 65 |
offset: ['start end', 'end start'], |
| 66 |
} |
| 67 |
); |
| 68 |
}); |
| 69 |
}); |
| 70 |
|