当前位置:首页 > 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实现定位效果

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

vue实现弹幕

vue实现弹幕

Vue 实现弹幕功能 弹幕功能通常包括文字在屏幕上滚动显示的效果。以下是使用 Vue 实现弹幕的几种方法: 使用 CSS 动画实现基础弹幕 创建一个 Vue 组件,利用 CSS 的 @keyfra…

js实现滚动条效果

js实现滚动条效果

实现滚动条效果的方法 使用原生JavaScript实现滚动条 通过监听滚动事件,可以动态调整滚动条的位置和样式。以下是一个基础实现示例: // 获取DOM元素 const container = d…

jquery 效果

jquery 效果

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

h5所能实现的效果

h5所能实现的效果

H5能实现的效果 H5(HTML5)作为最新的HTML标准,具备强大的功能和丰富的API支持,能够实现多种现代Web应用效果。以下是H5的主要能力分类: 多媒体支持 H5原生支持音频和视频播放,无…

vue实现框架效果

vue实现框架效果

Vue实现框架效果的方法 使用Vue实现框架效果可以通过多种方式完成,包括使用Vue Router、动态组件或第三方UI库。以下是几种常见的方法: 使用Vue Router实现布局框架 Vue R…