animation-view.vue 1.7 KB
Newer Older
Y
yurj26 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
<template>
	<view v-show="!hidden" ref="animation">
	</view>
</template>

<script>
	import lottie from './lottie.js'
	export default {
		name: 'animation-view',
		props: {
			/**
			 * 动画资源地址,支持远程 URL 地址和本地绝对路径
			 */
			"path": {
				type: String,
				default: ""
			},
			/**
			 * 动画是否自动播放
			 */
			"autoplay": {
				type: Boolean,
				default: false
			},
			/**
			 * 动画是否循环播放
			 */
			"loop": {
				type: Boolean,
				default: false
			},
			/**
			 * 是否隐藏动画
			 */
			"hidden": {
				type: Boolean,
				default: true
			},
			/**
			 * 动画操作,可取值 play、pause、stop
			 */
			"action": {
				type: String,
				default: "play",
				validator: (value) => {
					return ['play', 'pause', 'stop'].includes(value)
				}
			}
		},
		watch: {
			path(val) {
				this.init()
			},
			action(val) {
				switch (val) {
					case "play":
						this.play()
						break
					case "pause":
						this.pause()
						break
					case "stop":
						this.stop()
						break
					default:
						break
				}
			}
		},
		data() {
			return {
				animation: null
			}
		},
		mounted() {
			this.init()
		},
		methods: {
			init() {
				if (this.animation) {
					this.animation.destroy()
				}
				// 初始化
				this.animation = lottie.loadAnimation({
					path: this.path,
					loop: this.loop,
					autoplay: this.autoplay,
					loop: this.loop,
					container: this.$refs.animation.$el
				})
				// 动画结束
				this.animation.onComplete = () => {
					this.$emit('bindended')
				}
			},
			play() {
				this.animation?.play()
			},
			pause() {
				this.animation?.pause()
			},
			stop() {
				this.animation?.stop()
			},
		}
	}
</script>