simplify mouse events
This commit is contained in:
parent
916344dcab
commit
88451b60a2
2 changed files with 13 additions and 13 deletions
|
|
@ -36,7 +36,7 @@
|
||||||
let progress = $state(0);
|
let progress = $state(0);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Story name="Basic">
|
<Story name="Demo">
|
||||||
<ScrollerLottie autoplay loop showDebugInfo />
|
<ScrollerLottie autoplay loop showDebugInfo />
|
||||||
</Story>
|
</Story>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -107,14 +107,17 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const hoverHandler = (event: MouseEvent) => {
|
function handleMouseEnter() {
|
||||||
if (!playOnHover || !lottiePlayer?.isLoaded) return;
|
if (playOnHover && lottiePlayer?.isLoaded) {
|
||||||
if (event.type === 'mouseenter') {
|
lottiePlayer.play();
|
||||||
lottiePlayer?.play();
|
|
||||||
} else if (event.type === 'mouseleave') {
|
|
||||||
lottiePlayer?.pause();
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
function handleMouseLeave() {
|
||||||
|
if (playOnHover && lottiePlayer?.isLoaded) {
|
||||||
|
lottiePlayer.pause();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
const shouldAutoplay = autoplay && !playOnHover;
|
const shouldAutoplay = autoplay && !playOnHover;
|
||||||
|
|
@ -149,12 +152,7 @@
|
||||||
dotLottieRefCallback(lottiePlayer);
|
dotLottieRefCallback(lottiePlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas.addEventListener('mouseenter', hoverHandler);
|
|
||||||
canvas.addEventListener('mouseleave', hoverHandler);
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
canvas.removeEventListener('mouseenter', hoverHandler);
|
|
||||||
canvas.removeEventListener('mouseleave', hoverHandler);
|
|
||||||
lottiePlayer?.destroy();
|
lottiePlayer?.destroy();
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
@ -391,6 +389,8 @@
|
||||||
bind:this={canvas}
|
bind:this={canvas}
|
||||||
bind:clientWidth={canvasWidth}
|
bind:clientWidth={canvasWidth}
|
||||||
bind:clientHeight={canvasHeight}
|
bind:clientHeight={canvasHeight}
|
||||||
|
onmouseenter={handleMouseEnter}
|
||||||
|
onmouseleave={handleMouseLeave}
|
||||||
></canvas>
|
></canvas>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue