当前位置:首页 > 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>

脚本部分

<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>

优化弹幕性能

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

// 在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>

弹幕轨道系统

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

vue实现简单弹幕效果

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实现放大效果

使用 CSS transform 实现放大效果 在 Vue 中实现放大效果可以通过 CSS 的 transform: scale() 属性。这种方法简单高效,适合大多数交互场景。 定义一个 CSS…

Vue实现弹幕漂浮效果

Vue实现弹幕漂浮效果

Vue实现弹幕弹幕漂浮效果 核心思路 通过动态生成弹幕DOM元素,利用CSS动画或JavaScript控制其从右向左移动,并通过Vue的数据驱动特性管理弹幕生命周期。 基础实现步骤 创建弹幕组件 定…

vue实现框架效果

vue实现框架效果

Vue 实现框架效果的方法 Vue.js 可以通过多种方式实现框架效果,包括使用内置指令、组件封装或第三方库。以下是几种常见的方法: 使用 Vue 指令实现基础框架效果 通过 Vue 的 v-if、…

vue实现定位效果

vue实现定位效果

使用Geolocation API实现定位 在Vue中可以通过浏览器内置的Geolocation API获取用户位置信息。需要在组件的mounted钩子中调用相关方法: mounted() {…

h5如何实现vr效果

h5如何实现vr效果

使用WebXR API实现VR效果 WebXR是浏览器中实现VR/AR的核心API,支持设备姿态追踪、渲染交互等功能。需在支持WebXR的设备(如Oculus、HTC Vive)或浏览器模拟环境中运行…

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…