当前位置:首页 > VUE

vue实现歌词效果

2026-01-19 22:37:55VUE

Vue实现歌词效果

数据准备

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

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

音频播放监听

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

vue实现歌词效果

<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过渡和动态样式实现平滑滚动:

vue实现歌词效果

<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'
    });
  }
}

性能优化

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

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 的 v-if、…

vue实现拖拉效果

vue实现拖拉效果

实现拖拽效果的基本方法 在Vue中实现拖拽效果可以通过原生HTML5的拖拽API或第三方库如vuedraggable来完成。以下是两种常见方法的详细说明。 使用HTML5拖拽API HTML5提供…

vue实现定位效果

vue实现定位效果

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

vue实现预览效果

vue实现预览效果

Vue 实现预览效果的方法 使用 v-html 指令实现简单预览 在 Vue 中可以通过 v-html 指令直接将 HTML 字符串渲染到页面上,适用于简单的富文本预览场景。 <templat…

js实现图片滚动效果

js实现图片滚动效果

使用CSS动画实现图片滚动 通过CSS的@keyframes和animation属性实现无缝滚动效果。将图片容器设置为横向排列,通过动画平移位置。 <style> .scroll-c…

jquery 效果

jquery 效果

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