当前位置:首页 > VUE

vue实现歌词效果

2026-01-19 22:37:55VUE

Vue实现歌词效果

数据准备

歌词通常以时间轴和文本形式存储,可采用数组或对象格式。例如:

lyrics: [
  { time: 0.5, text: "第一句歌词" },
  { time: 3.2, text: "第二句歌词" }
]

音频播放监听

通过audio元素的timeupdate事件实时获取当前播放时间:

<audio ref="audio" @timeupdate="handleTimeUpdate" src="music.mp3"></audio>
methods: {
  handleTimeUpdate() {
    const currentTime = this.$refs.audio.currentTime;
    this.updateLyric(currentTime);
  }
}

歌词高亮逻辑

遍历歌词数组,匹配当前时间点对应的歌词行:

updateLyric(currentTime) {
  for (let i = 0; i < this.lyrics.length; i++) {
    if (currentTime >= this.lyrics[i].time && 
        (i === this.lyrics.length - 1 || currentTime < this.lyrics[i + 1].time)) {
      this.currentLine = i;
      break;
    }
  }
}

滚动效果实现

使用CSS过渡和动态样式实现平滑滚动:

<div class="lyric-container" ref="container">
  <div 
    v-for="(line, index) in lyrics" 
    :key="index"
    :class="{ 'active': currentLine === index }"
  >
    {{ line.text }}
  </div>
</div>
.lyric-container {
  height: 300px;
  overflow-y: auto;
  transition: transform 0.3s ease;
}
.active {
  color: #ff0000;
  font-weight: bold;
}

自动滚动定位

通过计算偏移量使当前歌词始终居中:

scrollToCurrent() {
  const container = this.$refs.container;
  const activeItem = container.querySelector('.active');
  if (activeItem) {
    const offset = activeItem.offsetTop - container.offsetHeight / 2;
    container.scrollTo({
      top: offset,
      behavior: 'smooth'
    });
  }
}

性能优化

使用节流函数避免频繁触发滚动计算:

vue实现歌词效果

import { throttle } from 'lodash';
export default {
  methods: {
    updateLyric: throttle(function(currentTime) {
      // 原有逻辑
    }, 300)
  }
}

完整组件示例

<template>
  <div>
    <audio ref="audio" @timeupdate="handleTimeUpdate"></audio>
    <div class="lyric-wrapper">
      <div class="lyric-container" ref="container">
        <div 
          v-for="(line, index) in lyrics" 
          :key="index"
          :class="{ 'active': currentLine === index }"
        >
          {{ line.text }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentLine: 0,
      lyrics: [] // 从接口获取或直接定义
    }
  },
  methods: {
    handleTimeUpdate() {
      const currentTime = this.$refs.audio.currentTime;
      this.updateLyric(currentTime);
    },
    updateLyric(currentTime) {
      // 匹配逻辑
      this.scrollToCurrent();
    },
    scrollToCurrent() {
      // 滚动逻辑
    }
  }
}
</script>

标签: 效果歌词
分享给朋友:

相关文章

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 Vue.js 提供了多种方式来实现动态效果展示,以下是一些常见的方法: 使用 Vue 的过渡和动画系统 Vue 内置了 <transition> 和 <…

vue实现歌词滚动

vue实现歌词滚动

实现歌词滚动的基本思路 在Vue中实现歌词滚动效果,通常需要结合音频播放器的当前时间与歌词时间轴进行同步。核心逻辑包括解析歌词文件、匹配当前播放时间对应的歌词行、实现滚动动画效果。 歌词文件解析 歌…

vue实现旋转效果

vue实现旋转效果

使用CSS动画实现旋转 在Vue中可以通过CSS的@keyframes或transform属性实现旋转效果。在组件的<style>部分定义旋转动画: .rotate { animat…

vue实现删除效果

vue实现删除效果

Vue实现删除效果的方法 使用v-if或v-show控制显示 通过数据绑定和条件渲染指令,可以动态控制元素的显示与隐藏。v-if会完全移除DOM元素,v-show仅通过CSS控制显示。 <te…

vue实现iframe效果

vue实现iframe效果

使用 Vue 实现 iframe 效果 在 Vue 中实现 iframe 效果可以通过直接使用 <iframe> 标签或结合动态绑定实现更灵活的功能。以下是几种常见方法: 基础 ifra…