uniapp:实现弹幕效果

一个好用的弹幕效果插件,亲测有效,插件地址:弹幕动起来


<view class="danmu">
	<lff-barrage ref="lffBarrage" :maxTop="120" :minTop="10">lff-barrage>
view>
danmuList :[
	'一帆风顺',
	'二龙腾飞',
	'三羊开泰',
	'四季平安',
	'五福临门',
	'六六大顺',
	'七星高照',
	'八方来财',
	'九九同心',
	'十全十美!',
	'新年快乐'
]
onLoad() {
	let index = 0
	setInterval(() => {
		index++
		if(index < this.danmuList.length){
			this.$refs.lffBarrage.add({
				item: this.danmuList[index]
			});
		}
	},1000)
}

lff-barrage子组件内容
父级元素:
position: absolute在父级内部弹
position: fixed; 全屏弹幕

<template>
	<view style="overflow: hidden;position: absolute;width: 100%;height: 100%;pointer-events: none; top: 0;">
		<view class="danmu-li" v-for="(item,index) in listData" :class="item.type" :style="item.style" :key="index">
			<view class="danmu-inner">
				<view class="user-box">
					<view class="user-status cl1">
						{{item.item}}
					view>
				view>
			view>
		view>
	view>
template>
<script>
	export default {
		props: {
			//rightToLeft leftToRight leftBottom
			type: {
				type: String,
				default: 'rightToLeft'
			},
			minTime: {
				type: Number,
				default: 4
			},
			maxTime: {
				type: Number,
				default: 9
			},
			minTop: {
				type: Number,
				default: 0
			},
			maxTop: {
				type: Number,
				default: 240
			},
			hrackH: { //轨道高度
				type: Number,
				default: 40
			},
			noStacked:{ //不允许堆叠(暂不可用)
				type:Array,
				default(){
					return []
				}
			}
		},
		data() {
			return {
				listData: []
			}
		},
		mounted() {
			this.hrackNum = Math.floor((this.maxTop - this.minTop) / this.hrackH);
		},
		methods: {
			add(obj) {
				let data = {
					item: obj.item,
					id:Date.parse(new Date()),
					time: Math.ceil(Math.floor(Math.random() * (this.maxTime - this.minTime + 1) + this.minTime)),
					type: this.type
				}
				if (this.type === 'leftBottom') {
					let objData = {
						item: data.item,
						type: 'leftBottomEnter',
						style: {
							transition: `all 0.5s`,
							animationDuration: `0.5s`,
							transform: `translateX(0%)`,
							bottom: `${this.minTop}px`
						}
					}
					let listLen = this.listData.length;
					let hrackNum = this.hrackNum;
					for (let i in this.listData) {
						if(this.listData[i].status === 'reuse'){ //重用
							this.$set(this.listData,i,objData);
						}else if(this.listData[i].status === 'reset'){ //重置
							this.listData[i].style.transition = 'none';
							this.listData[i].style.bottom = 0;
							this.listData[i].status = 'reuse';
						}else if(this.listData[i].status === 'recycle'){ //回收
							this.listData[i].type = 'leftBottomExit';
							this.listData[i].status = 'reset';
						}else{
							this.listData[i].style.bottom = parseInt(this.listData[i].style.bottom) + this.hrackH + 'px';
						}
						if(parseInt(this.listData[i].style.bottom) >= (this.maxTop - this.hrackH) && this.listData[i].status !== 'reset'){ //需要回收
							this.listData[i].status = 'recycle';
						}
					}
					if(listLen < hrackNum + 2){
						this.listData.push(objData);
					}
				} else if (this.type === 'rightToLeft' || this.type === 'leftToRight') {
					let objData = this.horStacked(data);
					for (let i in this.listData) {
						if (this.listData[i].delTime <= Date.parse(new Date())) {
							this.repaint(i, objData.type);
							objData.type = '';
							this.$set(this.listData, i, objData);
							return
						}
					}
					this.listData.push(objData);
				}
			},
			horStacked(data){
				return {
					item: data.item,
					type: this.type,
					style: {
						animationDuration: `${data.time}s`,
						top: `${Math.ceil(Math.random() * (this.maxTop - this.minTop + 1) + this.minTop)}px`
					},
					delTime: Date.parse(new Date()) + data.time * 1200
				}
			},
			repaint(index, type) {
				setTimeout(() => {
					this.listData[index].type = type;
				}, 100)
			}
		}

	}
script>
<style>

style>
<style lang="scss">
	@keyframes leftBottomEnter {
		0% {
			transform: translateY(100%);
			opacity: 0;
		}

		100% {
			transform: translateY(0%);
			opacity: 1;
		}
	}

	@keyframes leftBottomExit {
		0% {
			transform: translateY(0%);
			opacity: 1;
		}
		
		100% {
			transform: translateY(-200%);
			opacity: 0;
		}
	}

	@keyframes leftToRight {
		0% {
			transform: translateX(-100%);
		}

		100% {
			transform: translateX(100%);
		}
	}

	@keyframes rightToLeft {
		0% {
			transform: translateX(100%);
		}

		100% {
			transform: translateX(-100%);
		}
	}

	.danmu-li {
		position: absolute;
		width: 100%;
		transform: translateX(100%);
		animation-timing-function: linear;

		&.leftBottomEnter {
			animation-name: leftBottomEnter;
		}
		&.leftBottomExit{
			animation-name: leftBottomExit;
			animation-fill-mode: forwards;
		}

		&.rightToLeft {
			animation-name: rightToLeft;
		}

		&.leftToRight {
			animation-name: leftToRight;
		}

		.danmu-inner {
			display: inline-block;

			.user-box {
				display: flex;
				background: rgba(0, 0, 0, 0.3);
				border-radius: 32rpx;
				align-items: center;
				padding: 10rpx 30rpx;

				.user-status {
					white-space: nowrap;
					font-size: 26rpx;
					font-weight: 400;
					color: rgba(255, 255, 255, 1);
				}
			}
		}
	}
style>

如何使用

import lffBarrage from '@/components/lff-barrage/lff-barrage.vue'
components:{lffBarrage},
methods:{
	colrdo(){ //插入一条弹幕
		this.$refs.lffBarrage.add({item:'你好呀小伙子'});
	}
}
<lff-barrage ref="lffBarrage">lff-barrage>

你可能感兴趣的:(uniapp,uni-app,java,算法)