当前位置:首页 > VUE

vue实现歌词滚动

2026-02-11 09:48:46VUE

Vue 实现歌词滚动功能

歌词滚动是音乐播放器中常见的功能,可以通过 Vue 的动态数据绑定和动画效果实现。以下是实现歌词滚动的几种方法。

使用 CSS 动画和动态样式

通过绑定当前播放时间的歌词行高亮样式,结合 CSS 的滚动效果实现平滑过渡。

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

<script>
export default {
  data() {
    return {
      lyrics: [
        { time: 0, text: "第一行歌词" },
        { time: 5, text: "第二行歌词" },
        { time: 10, text: "第三行歌词" }
      ],
      currentLine: 0,
      currentTime: 0
    };
  },
  methods: {
    updateCurrentLine(time) {
      this.currentTime = time;
      for (let i = 0; i < this.lyrics.length; i++) {
        if (time >= this.lyrics[i].time) {
          this.currentLine = i;
        }
      }
      this.scrollToCurrentLine();
    },
    scrollToCurrentLine() {
      const container = this.$refs.lyricsContainer;
      const activeLine = container.querySelector('.active');
      if (activeLine) {
        container.scrollTo({
          top: activeLine.offsetTop - container.offsetHeight / 2,
          behavior: 'smooth'
        });
      }
    }
  }
};
</script>

<style>
.lyrics-container {
  height: 300px;
  overflow-y: auto;
  scroll-behavior: smooth;
}
.lyric-line {
  padding: 8px;
  text-align: center;
}
.lyric-line.active {
  color: #42b983;
  font-weight: bold;
}
</style>

结合第三方库实现

如果需要更复杂的滚动效果,可以使用第三方库如 better-scrollvue-seamless-scroll

安装 better-scroll

npm install better-scroll

实现代码:

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

<script>
import BScroll from 'better-scroll';

export default {
  data() {
    return {
      lyrics: [],
      currentLine: 0,
      bs: null
    };
  },
  mounted() {
    this.initScroll();
  },
  methods: {
    initScroll() {
      this.bs = new BScroll(this.$refs.lyricsWrapper, {
        probeType: 3,
        click: true
      });
    },
    scrollToCurrentLine() {
      const activeLine = this.$refs.lyricsWrapper.querySelector('.active');
      if (activeLine) {
        this.bs.scrollToElement(activeLine, 300);
      }
    }
  }
};
</script>

动态解析歌词文件

通常歌词文件是 LRC 格式,需要解析时间戳和歌词内容。

function parseLRC(lrcText) {
  const lines = lrcText.split('\n');
  const result = [];
  const timeRegex = /\[(\d+):(\d+\.\d+)\]/;

  lines.forEach(line => {
    const match = timeRegex.exec(line);
    if (match) {
      const minutes = parseFloat(match[1]);
      const seconds = parseFloat(match[2]);
      const time = minutes * 60 + seconds;
      const text = line.replace(timeRegex, '').trim();
      result.push({ time, text });
    }
  });

  return result.sort((a, b) => a.time - b.time);
}

与音频播放同步

通过监听音频的 timeupdate 事件,实时更新当前歌词行。

const audio = new Audio('song.mp3');
audio.addEventListener('timeupdate', () => {
  this.updateCurrentLine(audio.currentTime);
});

以上方法可以根据实际需求组合使用,实现流畅的歌词滚动效果。关键点在于准确解析歌词时间戳、高效更新当前行以及平滑滚动到目标位置。

vue实现歌词滚动

标签: 歌词vue
分享给朋友:

相关文章

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <templ…

vue实现星星

vue实现星星

实现星星评分的Vue组件 使用Vue实现星星评分功能可以通过组件化方式完成,以下是一个完整的实现方案: 模板部分 <template> <div class="star-…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性可…

vue实现截图

vue实现截图

Vue 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CS…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存…