vue 弹幕插件

本文介绍了一款基于Vue的弹幕插件的使用方法,包括安装步骤、配置选项及演示示例。通过具体代码展示了如何实现弹幕效果,并提供了不同颜色风格的设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、概述

弹幕是中国较受欢迎的弹幕展示方式。

先来看一下效果图

二、安装插件

npm install vue-baberrage -D

官方链接:https://blog.chenhaotaishuaile.com/vue-baberrage/

中文文档:https://github.com/superhos/vue-baberrage/blob/master/docs/zh/README.md

三、演示效果

test.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: '~',
        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: "人生若只如初见",
            time: 1,
            barrageStyle: 'red'
          },
          {
            id: 2,
            avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
            msg: "何事秋风悲画扇",
            time: 2,
            barrageStyle: 'green'
          },
          {
            id: 3,
            avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
            msg: "等闲变却故人心",
            time: 3,
            barrageStyle: 'normal'
          },
          {
            id: 4,
            avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
            msg: "却道故人心易变",
            time: 4,
            barrageStyle: 'blue'
          },
          {
            id: 5,
            avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
            msg: "骊山语罢清宵半",
            time: 5,
            barrageStyle: 'yellow'
          },
          {
            id: 6,
            avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
            msg: "泪雨霖铃终不怨",
            time: 6,
            barrageStyle: 'normal'
          },
          {
            id: 7,
            avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
            msg: "何如薄幸锦衣郎",
            time: 7,
            barrageStyle: 'red'
          },
          {
            id: 8,
            avatar: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3064584167,3502823640&fm=26&gp=0.jpg',
            msg: "比翼连枝当日愿",
            time: 8,
            barrageStyle: 'normal'
          },
        ];
        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="scss" scoped>
  .barrages-drop {
    /deep/ .baberrage-lane{
      /deep/ .blue .normal{
        border-radius: 100px;
        background: #e6ff75;
        color: #fff;
      }
      /deep/ .green .normal{
        border-radius: 100px;
        background: #75ffcd;
        color: #fff;
      }
      /deep/ .red .normal{
        border-radius: 100px;
        background: #e68fba;
        color: #fff;
      }
      /deep/ .yellow .normal{
        border-radius: 100px;
        background: #dfc795;
        color: #fff;
      }
      .baberrage-stage {
        position: absolute;
        width: 100%;
        height: 212px;
        overflow: hidden;
        top: 0;
        margin-top: 130px;
      }
    }

  }
</style>
View Code

关于如何使用,在中文文档中有详细说明,这里不做重复。

本文参考链接:

https://www.jb51.net/article/172830.htm

### 推荐的 Vue3 弹幕插件及其实现方案 #### 使用 `vue-danmaku` 组件 对于 Vue3 的弹幕功能,可以考虑使用 `vue-danmaku` 插件[^3]。该组件支持多种类型的弹幕显示方式(如顶部、底部、滚动等),并提供了灵活的配置选项。如果需要实现循环播放效果,可以通过监听 `play-end` 回调事件,在所有弹幕完全滚出屏幕后重新触发播放。 以下是简单的代码示例: ```javascript <template> <div id="app"> <vue-danmaku :options="danmakuOptions" @play-end="handlePlayEnd"></vue-danmaku> </div> </template> <script> import { defineComponent } from 'vue'; import VueDanmaku from 'vue-danmaku'; export default defineComponent({ components: { VueDanmaku, }, data() { return { danmakuOptions: { comments: [ { text: '这是一条弹幕', color: '#fff', mode: 1, size: 25 }, // 滚动模式 { text: '这是另一条弹幕', color: '#f00', mode: 0, size: 25 }, // 静态模式 ], width: window.innerWidth, height: 400, }, }; }, methods: { handlePlayEnd() { this.$refs.danmaku.play(); // 循环播放逻辑 }, }, }); </script> ``` #### 自定义实现方案 如果没有找到完全满足需求的第三方库,也可以通过自定义 CSS 动画和 JavaScript 来实现弹幕功能。核心思路是利用定时器动态创建 DOM 节点,并配合动画属性控制其移动轨迹。 以下是一个基础实现的例子: ```html <div class="video-container"> <div v-for="(item, index) in bulletComments" :key="index" class="bullet-comment" :style="{ animationDuration: item.duration + 's' }">{{ item.text }}</div> </div> ``` ```css .video-container { position: relative; overflow: hidden; } .bullet-comment { position: absolute; white-space: nowrap; animation-name: scroll-left; animation-timing-function: linear; animation-fill-mode: forwards; } @keyframes scroll-left { from { transform: translateX(100%); } to { transform: translateX(-100%); } } ``` ```javascript data() { return { bulletComments: [ { text: '欢迎来到直播间!', duration: 8 }, { text: '主播好漂亮!', duration: 6 }, ], }; }, mounted() { const containerWidth = document.querySelector('.video-container').offsetWidth; setInterval(() => { this.bulletComments.push({ text: '新弹幕来了!', duration: Math.random() * 5 + 5 }); }, 2000); } ``` 以上方法可以根据实际场景调整样式和行为,灵活性较高。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值