当前位置:首页 > VUE

vue实现视频进度加载

2026-01-22 08:36:58VUE

实现视频进度加载的基本思路

在Vue中实现视频进度加载通常涉及监听视频的timeupdate事件,计算当前播放时间与总时长的比例,并更新进度条显示。核心是通过<video>元素的API和自定义进度条组件完成交互。

监听视频播放进度

通过@timeupdate事件监听视频播放进度变化,计算当前进度百分比:

<template>
  <div>
    <video 
      ref="videoPlayer" 
      @timeupdate="updateProgress" 
      src="your-video.mp4"
    ></video>
    <div class="progress-bar" :style="{ width: progress + '%' }"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progress: 0
    };
  },
  methods: {
    updateProgress() {
      const video = this.$refs.videoPlayer;
      this.progress = (video.currentTime / video.duration) * 100;
    }
  }
};
</script>

自定义进度条交互

允许用户点击进度条跳转到指定位置,需处理click事件并更新视频的currentTime

<template>
  <div class="progress-container" @click="seek">
    <div class="progress-bar" :style="{ width: progress + '%' }"></div>
  </div>
</template>

<script>
export default {
  methods: {
    seek(event) {
      const progressContainer = event.currentTarget;
      const clickPosition = event.offsetX;
      const containerWidth = progressContainer.clientWidth;
      const percentage = (clickPosition / containerWidth) * 100;
      this.$refs.videoPlayer.currentTime = (percentage / 100) * this.$refs.videoPlayer.duration;
    }
  }
};
</script>

显示缓冲进度

通过<video>buffered属性获取已缓冲的范围,并更新UI:

updateBufferProgress() {
  const video = this.$refs.videoPlayer;
  if (video.buffered.length > 0) {
    const bufferedEnd = video.buffered.end(video.buffered.length - 1);
    this.bufferProgress = (bufferedEnd / video.duration) * 100;
  }
}

完整组件示例

结合进度条、缓冲条和交互的完整组件:

vue实现视频进度加载

<template>
  <div>
    <video 
      ref="videoPlayer" 
      @timeupdate="updateProgress" 
      @progress="updateBufferProgress"
      src="your-video.mp4"
    ></video>
    <div class="progress-container" @click="seek">
      <div class="buffer-bar" :style="{ width: bufferProgress + '%' }"></div>
      <div class="progress-bar" :style="{ width: progress + '%' }"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progress: 0,
      bufferProgress: 0
    };
  },
  methods: {
    updateProgress() {
      const video = this.$refs.videoPlayer;
      this.progress = (video.currentTime / video.duration) * 100;
    },
    updateBufferProgress() {
      const video = this.$refs.videoPlayer;
      if (video.buffered.length > 0) {
        const bufferedEnd = video.buffered.end(video.buffered.length - 1);
        this.bufferProgress = (bufferedEnd / video.duration) * 100;
      }
    },
    seek(event) {
      const progressContainer = event.currentTarget;
      const clickPosition = event.offsetX;
      const percentage = (clickPosition / progressContainer.clientWidth) * 100;
      this.$refs.videoPlayer.currentTime = (percentage / 100) * this.$refs.videoPlayer.duration;
    }
  }
};
</script>

<style>
.progress-container {
  height: 5px;
  background: #ddd;
  cursor: pointer;
  position: relative;
}
.progress-bar {
  height: 100%;
  background: #42b983;
  position: absolute;
  z-index: 2;
}
.buffer-bar {
  height: 100%;
  background: #ccc;
  position: absolute;
  z-index: 1;
}
</style>

注意事项

  • 确保视频元数据已加载后再操作duration属性,可通过@loadedmetadata事件处理。
  • 移动端需额外处理触摸事件(@touchstart@touchmove)。
  • 若使用第三方UI库(如Element UI),可直接使用其进度条组件并绑定视频事件。

通过以上方法,可以实现一个交互完整的视频进度加载功能,支持播放进度显示、缓冲状态提示和用户跳转操作。

标签: 进度加载
分享给朋友:

相关文章

js实现页面加载

js实现页面加载

监听 DOMContentLoaded 事件 使用 DOMContentLoaded 事件可以在 HTML 文档完全加载和解析后触发,无需等待样式表、图像等外部资源加载完成。 document.…

vue实现圆环进度

vue实现圆环进度

Vue 实现圆环进度的方法 使用 SVG 实现圆环进度 SVG 是实现圆环进度的常用方法,通过 stroke-dasharray 和 stroke-dashoffset 属性可以轻松控制进度条的显示。…

vue实现无限加载

vue实现无限加载

Vue 实现无限加载的方法 无限加载(Infinite Scroll)是一种常见的用户体验优化方式,适用于数据量较大的列表或瀑布流场景。以下是几种在 Vue 中实现无限加载的常用方法。 监听滚动事件…

vue实现加载更多

vue实现加载更多

Vue 实现加载更多功能 在 Vue 中实现加载更多功能通常涉及监听滚动事件或点击按钮触发加载更多数据。以下是几种常见的实现方式: 滚动监听实现无限滚动 通过监听滚动事件,当用户滚动到页面底部时自动…

vue懒加载实现

vue懒加载实现

Vue懒加载的实现方法 懒加载(Lazy Loading)是优化前端性能的重要技术,Vue中可通过多种方式实现组件或资源的按需加载。 路由懒加载 使用Vue Router时,可通过动态导入语法实现路…

vue实现图片预加载

vue实现图片预加载

图片预加载的实现方法 在Vue中实现图片预加载可以提升用户体验,避免图片加载延迟导致的空白或闪烁。以下是几种常见的方法: 使用JavaScript的Image对象 创建一个Image对象并设置其sr…