当前位置:首页 > VUE

vue实现弹幕移动效果

2026-02-22 06:15:15VUE

实现弹幕移动效果的方法

在Vue中实现弹幕移动效果,可以通过CSS动画或JavaScript动态计算位置来实现。以下是两种常见的实现方式:

CSS动画实现

使用CSS的@keyframestransform属性创建平移动画,结合Vue的动态渲染控制弹幕元素。

<template>
  <div class="danmu-container">
    <div 
      v-for="(item, index) in danmuList" 
      :key="index" 
      class="danmu-item"
      :style="{
        top: `${item.top}px`,
        animation: `move ${item.speed}s linear forwards`
      }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      danmuList: []
    }
  },
  methods: {
    addDanmu(text) {
      this.danmuList.push({
        text,
        top: Math.random() * 200,
        speed: 5 + Math.random() * 5
      })

      setTimeout(() => {
        this.danmuList.shift()
      }, 6000)
    }
  }
}
</script>

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

.danmu-item {
  position: absolute;
  white-space: nowrap;
  color: white;
  text-shadow: 1px 1px 2px black;
}

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

JavaScript动态计算实现

vue实现弹幕移动效果

通过requestAnimationFrame实现更精确的控制,适合需要复杂交互的场景。

<template>
  <div class="danmu-container" ref="container">
    <div 
      v-for="(item, index) in danmuList" 
      :key="index" 
      class="danmu-item"
      :style="{
        top: `${item.top}px`,
        left: `${item.left}px`
      }"
    >
      {{ item.text }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      danmuList: [],
      animationId: null
    }
  },
  mounted() {
    this.startAnimation()
  },
  beforeDestroy() {
    cancelAnimationFrame(this.animationId)
  },
  methods: {
    addDanmu(text) {
      this.danmuList.push({
        text,
        top: Math.random() * 200,
        left: this.$refs.container.offsetWidth,
        speed: 2 + Math.random() * 3
      })
    },
    startAnimation() {
      const animate = () => {
        this.danmuList.forEach(item => {
          item.left -= item.speed
        })

        this.danmuList = this.danmuList.filter(item => 
          item.left > -100
        )

        this.animationId = requestAnimationFrame(animate)
      }
      animate()
    }
  }
}
</script>

性能优化建议

对于大量弹幕场景,可以考虑以下优化措施:

vue实现弹幕移动效果

使用虚拟滚动技术,只渲染可视区域内的弹幕元素

// 示例代码片段
computed: {
  visibleDanmus() {
    return this.danmuList.filter(item => 
      item.left < this.containerWidth && 
      item.left > -item.width
    )
  }
}

采用Canvas渲染代替DOM操作,大幅提升性能

// 示例代码片段
drawDanmu() {
  const ctx = this.$refs.canvas.getContext('2d')
  ctx.clearRect(0, 0, width, height)

  this.danmuList.forEach(item => {
    ctx.fillText(item.text, item.x, item.y)
    item.x -= item.speed
  })
}

交互增强功能

实现点击暂停、速度调节等交互功能

methods: {
  togglePause() {
    this.isPaused = !this.isPaused
    if (!this.isPaused) {
      this.startAnimation()
    } else {
      cancelAnimationFrame(this.animationId)
    }
  },
  changeSpeed(factor) {
    this.danmuList.forEach(item => {
      item.speed *= factor
    })
  }
}

以上实现方式可根据具体需求选择或组合使用,CSS动画方案简单高效,JavaScript方案则提供更多控制灵活性。

标签: 效果弹幕
分享给朋友:

相关文章

vue实现基金效果

vue实现基金效果

Vue 实现基金效果 在 Vue 中实现基金效果,通常指的是模拟基金的增长、波动或可视化展示。以下是几种常见的实现方式: 数据绑定与动态更新 通过 Vue 的数据绑定特性,可以动态展示基金净值的变化…

vue实现动画效果

vue实现动画效果

Vue 实现动画效果的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及 CSS 动画。以下是几种常见的方法: 使用 Vue 内置的过渡系统 Vue 的 <tra…

vue实现多选效果

vue实现多选效果

Vue 实现多选效果的方法 使用 v-model 绑定数组 在 Vue 中,可以通过 v-model 绑定一个数组来实现多选效果。适用于复选框组(checkbox)或下拉多选(select multi…

vue实现框选效果

vue实现框选效果

实现框选效果的基本思路 在Vue中实现框选效果,通常需要监听鼠标事件,记录起始和结束位置,动态绘制一个矩形框,并根据框选范围判断哪些元素被选中。以下是具体实现方法: 监听鼠标事件 在Vue组…

vue实现multiselect效果

vue实现multiselect效果

Vue 实现多选(Multiselect)效果 使用第三方库 vue-multiselect 安装 vue-multiselect 库: npm install vue-multiselec…

vue实现点击高亮效果

vue实现点击高亮效果

实现点击高亮效果的方法 在Vue中实现点击高亮效果可以通过多种方式完成,以下是几种常见的方法: 方法一:使用动态class绑定 通过v-bind:class或简写:class动态切换高亮样式类。…