当前位置:首页 > VUE

vue实现简单弹幕效果

2026-01-20 18:46:37VUE

实现基础弹幕效果

在Vue中实现弹幕效果需要结合CSS动画和动态数据渲染。以下是一个基础实现方案:

模板部分

<template>
  <div class="danmu-container">
    <div 
      v-for="(item, index) in danmuList" 
      :key="index"
      class="danmu-item"
      :style="{
        top: `${item.top}px`,
        color: item.color,
        animationDuration: `${item.speed}s`
      }"
    >
      {{ item.text }}
    </div>
    <input v-model="inputText" @keyup.enter="sendDanmu" />
  </div>
</template>

脚本部分

vue实现简单弹幕效果

<script>
export default {
  data() {
    return {
      danmuList: [],
      inputText: '',
      colors: ['#ff0000', '#00ff00', '#0000ff', '#ffff00', '#ff00ff']
    }
  },
  methods: {
    sendDanmu() {
      if (!this.inputText.trim()) return

      const newDanmu = {
        text: this.inputText,
        top: Math.random() * 200,
        color: this.colors[Math.floor(Math.random() * this.colors.length)],
        speed: 5 + Math.random() * 5
      }

      this.danmuList.push(newDanmu)
      this.inputText = ''

      // 移除已完成动画的弹幕
      setTimeout(() => {
        this.danmuList.shift()
      }, newDanmu.speed * 1000)
    }
  }
}
</script>

样式设计

弹幕样式需要结合CSS动画实现横向移动效果:

<style>
.danmu-container {
  position: relative;
  width: 100%;
  height: 300px;
  overflow: hidden;
  background: #000;
}

.danmu-item {
  position: absolute;
  white-space: nowrap;
  font-size: 20px;
  animation: danmuMove linear;
  animation-fill-mode: forwards;
}

@keyframes danmuMove {
  from {
    transform: translateX(100%);
  }
  to {
    transform: translateX(-100%);
  }
}
</style>

优化弹幕性能

当弹幕数量较多时,需要考虑性能优化:

vue实现简单弹幕效果

// 在data中添加配置
data() {
  return {
    maxDanmu: 50, // 最大同时显示弹幕数
    // ...其他data
  }
}

// 修改发送方法
sendDanmu() {
  if (!this.inputText.trim()) return

  // 超过最大数量时移除最早的一条
  if (this.danmuList.length >= this.maxDanmu) {
    this.danmuList.shift()
  }

  // ...原有创建弹幕逻辑
}

添加弹幕碰撞检测

防止弹幕重叠可以提高视觉效果:

methods: {
  getRandomTop() {
    const top = Math.random() * 200
    const occupied = this.danmuList.filter(
      item => Math.abs(item.top - top) < 30
    ).length

    return occupied > 0 ? this.getRandomTop() : top
  },

  sendDanmu() {
    // ...其他逻辑
    const newDanmu = {
      // ...其他属性
      top: this.getRandomTop()
    }
    // ...后续逻辑
  }
}

实现弹幕暂停功能

添加控制弹幕播放状态的交互:

<template>
  <div>
    <!-- ...原有内容 -->
    <button @click="togglePause">{{ isPaused ? '播放' : '暂停' }}</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isPaused: false
    }
  },
  methods: {
    togglePause() {
      this.isPaused = !this.isPaused
      const danmus = document.querySelectorAll('.danmu-item')
      danmus.forEach(el => {
        el.style.animationPlayState = this.isPaused ? 'paused' : 'running'
      })
    }
  }
}
</script>

弹幕轨道系统

更专业的实现可以使用固定轨道:

data() {
  return {
    tracks: [50, 100, 150, 200], // 轨道位置
    trackStatus: {} // 记录轨道占用状态
  }
},
methods: {
  getAvailableTrack() {
    const availableTracks = this.tracks.filter(
      track => !this.trackStatus[track] || 
              Date.now() - this.trackStatus[track] > 3000
    )

    if (availableTracks.length) {
      const track = availableTracks[0]
      this.trackStatus[track] = Date.now()
      return track
    }

    return this.tracks[Math.floor(Math.random() * this.tracks.length)]
  }
}

标签: 效果简单
分享给朋友:

相关文章

vue实现预览效果

vue实现预览效果

实现预览效果的常见方法 在Vue中实现预览效果可以通过多种方式完成,以下列举几种典型场景和对应的实现方案: 图片/文件预览 使用<input type="file">结合FileRead…

vue实现过渡效果

vue实现过渡效果

Vue 过渡效果实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于在元素或组件进入/离开 DOM 时应用过渡效果。以…

vue 实现分页效果

vue 实现分页效果

使用 Element UI 实现分页 Element UI 提供了现成的分页组件 el-pagination,适合快速集成到 Vue 项目中。 安装 Element UI: npm install…

vue简单功能实现

vue简单功能实现

Vue 简单功能实现示例 以下是一个基于 Vue 3 的简单计数器功能实现,包含模板、脚本和样式部分: <template> <div class="counter">…

vue实现过度效果

vue实现过度效果

Vue 过渡效果实现方法 Vue 提供了内置的 <transition> 和 <transition-group> 组件,用于在元素或组件进入/离开 DOM 时应用过渡效果。…

vue实现菜单效果

vue实现菜单效果

Vue实现菜单效果的方法 基础菜单实现 使用Vue的v-for指令和v-show/v-if可以快速实现基础菜单。通过数据驱动的方式渲染菜单项,结合事件处理实现交互。 <template>…