PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.3
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.3
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / blocks / lottie / frontend.js

frontend.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.3, at gutenberg/blocks/lottie/frontend.js

70 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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