如何将 Lottie Interactivity 与 Svelte 集成

发布于 2025-01-14 15:50:03 字数 465 浏览 2 评论 0原文

我正在尝试将 Lottie Interactivity 集成到我的 Svelte 代码中。不幸的是,开箱即用的它会抛出错误代码。

当你深入研究他们的文档时,他们提供了 React 和 Vue 的钩子 - 但没有 Svelte!

我对 Svelte 还比较陌生,所以如果人们甚至可以提供有关如何开始将其集成到 Svelte 中的指示,我将不胜感激!

(在有人问之前,我说的是 Lottie Interactivity,而不是 Lottie Player。)

I'm trying to integrate Lottie Interactivity into my Svelte code. Unfortunately, out-of-the-box it throws error codes.

When you dig into their docs, they provide hooks for React and Vue — but no Svelte!

I'm still relatively new with Svelte, so if people can even provide pointers as to how to get started with integrating this into Svelte, I would greatly appreciate this!

(And before anyone asks — I'm talking about Lottie Interactivity, not Lottie Player.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

国产ˉ祖宗 2025-01-21 15:50:04

看起来您必须侦听加载事件才能创建动画 REPL

<script>
    import '@lottiefiles/lottie-player'
    import { create } from '@lottiefiles/lottie-interactivity'

    function handleLoad(event) {
        create({
            mode: 'scroll',
            player: '#firstLottie',
            actions: [
                {
                    visibility: [0, 1],
                    type: 'seek',
                    frames: [0, 100],
                },
            ],
        });
    }
</script>

<div>   
    <lottie-player
        id="firstLottie"                     
        src="https://assets2.lottiefiles.com/packages/lf20_i9mxcD.json"
        style="width:400px; height: 400px;"
        on:load={handleLoad}
    >
    </lottie-player>
</div>

<style>
    div {
        height: 2000px;
        padding-top: 500px;
    }
</style>

Looks like you have to listen for the load event to create the animation REPL

<script>
    import '@lottiefiles/lottie-player'
    import { create } from '@lottiefiles/lottie-interactivity'

    function handleLoad(event) {
        create({
            mode: 'scroll',
            player: '#firstLottie',
            actions: [
                {
                    visibility: [0, 1],
                    type: 'seek',
                    frames: [0, 100],
                },
            ],
        });
    }
</script>

<div>   
    <lottie-player
        id="firstLottie"                     
        src="https://assets2.lottiefiles.com/packages/lf20_i9mxcD.json"
        style="width:400px; height: 400px;"
        on:load={handleLoad}
    >
    </lottie-player>
</div>

<style>
    div {
        height: 2000px;
        padding-top: 500px;
    }
</style>

故事还在继续 2025-01-21 15:50:04

在第一页渲染期间它可能会说

500
内部错误

,它可能会在控制台中显示此错误: 。

参考错误:窗口未定义

为了消除这个问题,我们需要在 onMount 方法中导入包。这是 REPL

<script>
    import { onMount } from 'svelte';
    onMount(async()=>{
        await import('@lottiefiles/lottie-player')
        // @ts-ignore
        const {create} = await import('@lottiefiles/lottie-interactivity')
        handleLoad(create) 
    })
    // @ts-ignore
    const handleLoad =  (method)=>{
        method({
            mode: 'scroll',
            player: '#firstLottie',
            actions: [
                {
                    visibility: [0, 1],
                    type: 'seek',
                    frames: [0, 500],
                },
            ],
        });
    }
</script>

<div> 
    <div style="height:400px"></div>  
    <lottie-player
        id="firstLottie"                     
        src="https://assets2.lottiefiles.com/packages/lf20_i9mxcD.json"
        style="width:400px; height: 400px;"
        on:load={handleLoad}
    >
    </lottie-player>
</div>

During first page render it may say

500
Internal Error

and it may show this error in the console: .

ReferenceError: window is not defined

To eliminate this issue, we need to import the packages in onMount method. Here is the REPL

<script>
    import { onMount } from 'svelte';
    onMount(async()=>{
        await import('@lottiefiles/lottie-player')
        // @ts-ignore
        const {create} = await import('@lottiefiles/lottie-interactivity')
        handleLoad(create) 
    })
    // @ts-ignore
    const handleLoad =  (method)=>{
        method({
            mode: 'scroll',
            player: '#firstLottie',
            actions: [
                {
                    visibility: [0, 1],
                    type: 'seek',
                    frames: [0, 500],
                },
            ],
        });
    }
</script>

<div> 
    <div style="height:400px"></div>  
    <lottie-player
        id="firstLottie"                     
        src="https://assets2.lottiefiles.com/packages/lf20_i9mxcD.json"
        style="width:400px; height: 400px;"
        on:load={handleLoad}
    >
    </lottie-player>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文