Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle multiple animations on the same page #49

Open
AdamBD opened this issue Jul 1, 2020 · 8 comments
Open

How to handle multiple animations on the same page #49

AdamBD opened this issue Jul 1, 2020 · 8 comments

Comments

@AdamBD
Copy link

AdamBD commented Jul 1, 2020

I am creating multiple lottie elements on the same page, but because the 'last' one added to the dom becomes the last this.anim only the last animation is working. What is the way to handle multiple animations on the same page correctly?

 handleAnimation: function (anim) {
        this.anim = anim;
      },
@cweachock
Copy link

hey there, we were having this issue as well trying to get two instances from an exported JSON file to play the same animation with 2 different paths one for desktop and one for mobile. Is there a way to have 2 lottie wrappers on the same page to handle multiple animations?

@codingspook
Copy link

codingspook commented Jul 30, 2020

Hi everyone, i think i found a workaround to solve this issue (in my case works properly). Here's my solution:

First i imported json data:

import * as downloadAnimationData from './assets/download.json'; 
import * as bookAnimationData from './assets/book.json';

then i created an object in data() like this:

animationsOptions: {
                    download: {
                        animationData: downloadAnimationData.default
                        // optionally you can add other option parameters here like: 
                        // autoplay: false
                    }, 
                    book: {
                        animationData: bookAnimationData.default
                    }
                },

In lottie wrapper element i changed :option property attribute to match my object schema:

<lottie :options="animationsOptions.book" :height="28" :width="28" v-on:animCreated="handleAnimation"/>

then i added an additional parameter tho the handleAnimation callback (and preserved $event passed object that carries animation data from lottie's animCreated) specifying the "type" of the animation:

<lottie :options="animationsOptions.book" :height="28" :width="28" v-on:animCreated="handleAnimation($event, 'book')"/>

so i changed my methods according to populate main data()'s anim object and select right anim in play() and stop() methods calls:

methods: {
            handleAnimation: function (anim, type) {
                this.anim[type] = anim;
            },
            play: function (type) {
                this.anim[type].play();
            },
            stop: function (type) {
                this.anim[type].stop();
            },
        }

Example of usage with autoplay: false and hover action

<a href="#" @mouseover="play('book')" @mouseleave="stop('book')"> <lottie :options="animationsOptions.book" :height="28" :width="28" v-on:animCreated="handleAnimation($event, 'book')"/> <span>Read more</span></a>

Hope that this can help someone :)

@Hruskasvestka
Copy link

Hey there! I found kinda same solution as @mdicampli but with different :options. I will try to describe step by step down below.

  1. Import vue-lottie and JSON data :
import Lottie from 'vue-lottie';
import * as downloadAnimationData from '../assets/icons-lottie/download.json';
import * as coinAnimationData from '../assets/icons-lottie/coin.json';
import * as arrowAnimationData from '../assets/icons-lottie/arrow.json';
  1. Create an object:
export default { 
  components: {
    Lottie
  },
  data() {
    return {
      defaultOptions: {
        download: {
          animationData: downloadAnimationData.default
        },
        coin: {
          animationData: coinAnimationData.default
        },
        arrow: {
          animationData: arrowAnimationData.default
        }
      },
      animationSpeed: 1
    };
  },
};
  1. And that relevant changes are coming in DOM:
<lottie
  :options="defaultOptions.download" // main issue
  :height="45"
  :width="45"
  style="margin: 0 20px 0 -10px;" // you can add your own style here
/>
<lottie
  :options="defaultOptions.coin" // main issue
  :height="45"
  :width="45"
  style="margin: 0 20px 0 -10px;"  // you can add your own style here
/>

Instead of using animationsOptions.download in :option use defaultOptions.download.

@yogsky
Copy link

yogsky commented Feb 14, 2021

Can anybody please confirm they are able to run 5 or more animations in the same page without page turning unresponsive ?
I tried to run 7 instances of with autoplay off, and boom my app crashes 🙁

@Hruskasvestka
Copy link

Can anybody please confirm they are able to run 5 or more animations in the same page without page turning unresponsive ?
I tried to run 7 instances of with autoplay off, and boom my app crashes 🙁

Yes, it's possible to implement many animations in the same page.

@sddsaw
Copy link

sddsaw commented Oct 26, 2021

有demo吗

@vedmant
Copy link

vedmant commented Dec 23, 2021

I used simpler solution:

LottieWrapper.vue

<template>
  <client-only>
    <Lottie v-bind="$attrs" :key="name" v-on="$listeners" />
  </client-only>
</template>

<script>
export default {
  components: {
    Lottie: process.client ? require('vue-lottie').default : null,
  },

  props: {
    name: { type: String, required: true },
  },
}
</script>

It needs unique name for each animation.

@sddsaw
Copy link

sddsaw commented Dec 23, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants