当前位置:首页 > VUE

vue实现视频进度加载

2026-01-22 08:36:58VUE

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

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

监听视频播放进度

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

vue实现视频进度加载

<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

vue实现视频进度加载

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

完整组件示例

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

<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),可直接使用其进度条组件并绑定视频事件。

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

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

相关文章

vue实现加载

vue实现加载

Vue 实现加载功能的方法 使用 v-if 或 v-show 控制加载状态 通过数据绑定控制加载动画的显示与隐藏。v-if 会完全移除 DOM 元素,v-show 只是切换 display 属性。…

vue实现懒加载的方法

vue实现懒加载的方法

Vue 实现懒加载的方法 路由懒加载 通过动态导入语法拆分路由组件,减少初始加载时间。适用于 Vue Router 配置: const routes = [ { path: '/hom…

vue前端实现下载进度

vue前端实现下载进度

Vue 前端实现下载进度的方法 使用 axios 的 onDownloadProgress 回调 在 axios 请求中,可以通过 onDownloadProgress 回调函数实时获取下载进度。该回…

jquery加载

jquery加载

jQuery 加载方法 使用 CDN 加载 jQuery 推荐通过公共 CDN(如 Google、Microsoft 或 jQuery 官方)加载,速度快且可能已被浏览器缓存。 <…

jquery 加载

jquery 加载

jQuery 加载方法 使用jQuery需要先将其库文件加载到网页中。以下是几种常见的加载方式: 通过CDN加载 推荐使用官方或公共CDN(内容分发网络)加载jQuery,速度快且可能被浏览器缓存…

加载jquery

加载jquery

加载 jQuery 的方法 通过 CDN 加载 推荐使用官方或公共 CDN(内容分发网络)加载 jQuery,速度快且稳定。将以下代码添加到 HTML 文件的 <head> 或 <b…