vue.js - 如何在 NuxtJS 中使用 lottie 来实现悬停、单击和所有功能?
问题描述
我正在使用“ vue-lottie ”包,关于如何使用它的信息不多。
我从Lordicons获得了 JSON 动画,它显示正确,但我无法使动画在悬停或单击时工作,只能循环或静态(无动画)。
我的组件:
<template>
<div>
<lottie
:options="lottieOptions"
:width="50"
/>
</div>
</template>
<script>
import lottie from "vue-lottie/src/lottie.vue";
import * as animationData from "~/assets/about.json";
export default {
components: {
lottie
},
data() {
return {
anim: null, // for saving the reference to the animation
lottieOptions: {
animationData: animationData.default,
loop: false,
autoplay: false,
}
};
},
methods: {
handleAnimation(anim) {
this.anim = anim;
},
stop() {
this.anim.stop();
},
play() {
this.anim.play();
},
pause() {
this.anim.pause();
},
}
};
</script>
我只在页面上使用导入组件:
...
<AboutIcon />
...
<script>
import AboutIcon from "~/components/AboutIcon.vue";
export default {
components: {
AboutIcon,
},
data() {
return {};
}
};
</script>
解决方案
从您提供的代码示例中,您忘记附加@animCreated="handleAnimation"
事件。
所以this.anim
实际上总是空的。
<template>
<div>
<lottie
:options="lottieOptions"
:width="50"
@animCreated="handleAnimation"
/>
</div>
</template>
然后您只需设置 a@mouseover="play"
即可在悬停时启动动画。
推荐阅读
- docker - docker swarm 上的 Kafka 一段时间后无法正常工作
- php - 在 WooCommerce 中调用布尔值的成员函数 get_items() 谢谢
- promela - 如何在promela中获取随机数据
- arrays - 使用索引数组并行写入数组
- java - 使用带有布尔输出的 Int 输入测试日期是否有效的类,以及如何测试(java)
- grails - 在 Groovy Spock 中导入别名,正常行为?
- python - 是否有可能描述“Rood”在不同年份/月份出现的频率?
- javascript - 如何在 PayPal 订阅 API 中获取用户的订阅状态
- java - 我在一次播放多个音频时遇到问题
- sql - 减去两个价格