当前位置:首页 > 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>

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

相关文章

h5所能实现的效果

h5所能实现的效果

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

vue实现气泡效果

vue实现气泡效果

实现气泡效果的方法 在Vue中实现气泡效果可以通过CSS动画、第三方库或自定义组件完成。以下是几种常见方法: 使用纯CSS动画 通过CSS的@keyframes和transform属性创建气泡上升动…

vue实现玻璃效果

vue实现玻璃效果

实现玻璃效果的方法 在Vue中实现玻璃效果(毛玻璃或磨砂玻璃)可以通过CSS的backdrop-filter属性结合Vue的动态样式绑定来实现。以下是具体步骤: 使用CSS backdrop-fil…

js实现点击效果

js实现点击效果

实现点击效果的JavaScript方法 使用addEventListener绑定点击事件 通过document.getElementById或document.querySelector获取DOM元素…

vue实现折叠效果

vue实现折叠效果

使用 v-show 或 v-if 实现基础折叠 通过 Vue 的指令 v-show 或 v-if 控制元素的显示与隐藏。v-show 通过 CSS 的 display 属性切换,v-if 直接操作 D…

vue实现抽屉效果

vue实现抽屉效果

Vue 实现抽屉效果的方法 使用 Vue 过渡动画和 CSS 在 Vue 中可以通过 v-if 或 v-show 控制抽屉的显示隐藏,结合 <transition> 组件实现平滑动画效果。…