当前位置:首页 > 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方案则提供更多控制灵活性。

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

相关文章

jquery 效果

jquery 效果

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。jQuery 提供了丰富的效果(Effects)功能,可以轻松实现元素的动态显…

vue实现轮播效果

vue实现轮播效果

使用Swiper插件实现轮播 安装Swiper及相关依赖: npm install swiper vue-awesome-swiper 组件代码示例: <template> <…

vue实现筛选效果

vue实现筛选效果

Vue 实现筛选效果的方法 基于计算属性的筛选 在 Vue 中可以通过计算属性实现数据的动态筛选。定义一个计算属性,根据用户输入的筛选条件返回过滤后的数据列表。 <template>…

vue实现拖动效果

vue实现拖动效果

使用 vuedraggable 库实现拖动 vuedraggable 是基于 Sortable.js 的 Vue 组件库,提供简单易用的拖拽功能。安装方式如下: npm install vuedra…

vue实现的效果

vue实现的效果

Vue.js 是一个流行的前端框架,用于构建用户界面和单页应用。以下是一些常见的 Vue 实现效果及其实现方式: 动态数据绑定 Vue 的核心特性之一是响应式数据绑定。通过将数据与 DOM 元素绑定…

vue实现卡片效果

vue实现卡片效果

Vue实现卡片效果的方法 使用Vue实现卡片效果可以通过多种方式完成,包括使用原生CSS、UI框架或自定义组件。以下是几种常见方法: 使用原生CSS和Vue组件 创建一个简单的卡片组件,结合CSS实…