vue實(shí)現(xiàn)彈幕功能
(1)效果


如上圖所示的效果,這里我們使用vue 制作。
(2)使用技術(shù) vue + vue-baberrage
1.安裝:
npm install vue-baberrage
2.引入
方式一:
import Vue from 'vue'
import { vueBaberrage } from 'vue-baberrage'
Vue.use(vueBaberrage)
方式二:
const vueBaberrage = request('vue-baberrage').vueBaberrage
方式三:
<script src="./dist/vue-baberrage.js"></script>
3.使用
HTML
<div id="app"> <vue-baberrage :isShow= "barrageIsShow" :barrageList = "barrageList" :loop = "barrageLoop" > </vue-baberrage> </div>
JS
import { MESSAGE_TYPE } from 'vue-baberrage'
export default {
name: 'app',
data () {
return {
msg: 'Hello vue-baberrage',
barrageIsShow: true,
currentId : 0,
barrageLoop: false,
barrageList: []
}
},
methods:{
addToList (){
this.barrageList.push({
id: ++this.currentId,
avatar: "./static/avatar.jpg",
msg: this.msg,
time: 5,
type: MESSAGE_TYPE.NORMAL,
});
...
(3)實(shí)例演示
這個(gè)封裝成一個(gè) vue的組件:
<template>
<div class="barrages-drop">
<vue-baberrage
:isShow="barrageIsShow"
:barrageList="barrageList"
:maxWordCount="maxWordCount"
:throttleGap="throttleGap"
:loop="barrageLoop"
:boxHeight="boxHeight"
:messageHeight="messageHeight"
>
</vue-baberrage>
</div>
</template>
<script>
import Vue from 'vue';
import { vueBaberrage, MESSAGE_TYPE } from 'vue-baberrage';
Vue.use(vueBaberrage);
export default {
name: 'Barrages',
data() {
return {
msg: '馬優(yōu)晨就是個(gè)辣雞~',
barrageIsShow: true,
messageHeight: 3,
boxHeight: 150,
barrageLoop: true,
maxWordCount: 3,
throttleGap: 5000,
barrageList: []
};
},
mounted() {
this.addToList();
},
methods: {
addToList() {
let list = [
{
id: 1,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 3,
barrageStyle: 'red'
},
{
id: 2,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 8,
barrageStyle: 'green'
},
{
id: 3,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 10,
barrageStyle: 'normal'
},
{
id: 4,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 5,
barrageStyle: 'blue'
},
{
id: 5,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 6,
barrageStyle: 'red'
},
{
id: 6,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 12,
barrageStyle: 'normal'
},
{
id: 7,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 5,
barrageStyle: 'red'
},
{
id: 8,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 5,
barrageStyle: 'normal'
},
{
id: 9,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 8,
barrageStyle: 'red'
},
{
id: 10,
avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
msg: this.msg,
time: 10,
barrageStyle: 'yellow'
}
];
list.forEach((v) => {
this.barrageList.push({
id: v.id,
avatar: v.avatar,
msg: v.msg,
time: v.time,
type: MESSAGE_TYPE.NORMAL,
barrageStyle: v.barrageStyle
});
});
}
}
};
</script>
<style lang="less">
.barrages-drop {
.blue {
border-radius: 100px;
background: #e6ff75;
color: #fff;
}
.green {
border-radius: 100px;
background: #75ffcd;
color: #fff;
}
.red {
border-radius: 100px;
background: #e68fba;
color: #fff;
}
.yellow {
border-radius: 100px;
background: #dfc795;
color: #fff;
}
.baberrage-stage {
position: absolute;
width: 100%;
height: 212px;
overflow: hidden;
top: 0;
margin-top: 130px;
}
}
</style>
github地址:
https://github.com/superhos/vue-baberrage/blob/master/docs/zh/README.md#plugin-options
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)彈幕功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Vue常見(jiàn)報(bào)錯(cuò)整理大全(從此報(bào)錯(cuò)不害怕)
寫代碼的過(guò)程中一定會(huì)遇到報(bào)錯(cuò),遇到報(bào)錯(cuò)不要擔(dān)心,認(rèn)真分析就可以解決報(bào)錯(cuò),同時(shí)積累經(jīng)驗(yàn),早日成為大牛,這篇文章主要給大家介紹了關(guān)于Vue常見(jiàn)報(bào)錯(cuò)整理的相關(guān)資料,需要的朋友可以參考下2022-08-08
vue實(shí)現(xiàn)密碼顯示與隱藏按鈕的自定義組件功能
本文通過(guò)兩種思路給大家介紹vue實(shí)現(xiàn)密碼顯示與隱藏按鈕的自定義組件功能,感興趣的朋友跟隨小編一起看看吧2019-04-04
Vue項(xiàng)目中實(shí)現(xiàn)無(wú)感Token刷新的示例
在前端項(xiàng)目中,Token用于用戶認(rèn)證和權(quán)限管理,文章介紹了在Vue項(xiàng)目中實(shí)現(xiàn)Token的無(wú)感刷新,包括Token刷新的原理、攔截器的應(yīng)用和處理Token過(guò)期的方法,感興趣的可以了解一下2024-11-11
vue中的路由傳值與重調(diào)本路由改變參數(shù)
這篇文章主要介紹了vue中的路由傳值與重調(diào)本路由改變參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue使用axios發(fā)送請(qǐng)求簡(jiǎn)單實(shí)現(xiàn)代碼
axios是一個(gè)基于Promise的,發(fā)送http請(qǐng)求的一個(gè)工具庫(kù),并不是vue中的第三方插件,使用時(shí)不能通過(guò)“Vue.use()”安裝插件,需要在原型上進(jìn)行綁定2023-04-04
vue頁(yè)面使用阿里oss上傳功能的實(shí)例(二)
本篇文章主要介紹了vue頁(yè)面使用阿里oss上傳功能的實(shí)例(二),主要介紹OSS管理控制臺(tái)設(shè)置訪問(wèn)權(quán)限、角色等,有興趣的可以了解一下2017-08-08
vue實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
在Vue項(xiàng)目中引入Echarts繪制K線圖的方法技巧
在Vue項(xiàng)目開(kāi)發(fā)中,數(shù)據(jù)可視化是一項(xiàng)重要的任務(wù),Echarts是一個(gè)由百度開(kāi)源的數(shù)據(jù)可視化庫(kù),提供了豐富的圖表類型和強(qiáng)大的交互功能,其中,K線圖常用于展示金融數(shù)據(jù)的走勢(shì),本文將詳細(xì)介紹如何在Vue項(xiàng)目中引入Echarts并繪制K線圖,需要的朋友可以參考下2025-04-04

