当前位置:首页 > 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 实现异步加载的方法 使用动态 import 实现组件懒加载 在 Vue 中可以通过动态 import() 语法实现组件的异步加载,这会使得组件在需要时才被加载,减少初始加载时间。 const…

vue前端实现下载进度

vue前端实现下载进度

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

vue2实现图片懒加载

vue2实现图片懒加载

实现图片懒加载的方法 在Vue2中实现图片懒加载可以通过以下方法完成,核心原理是监听图片是否进入可视区域,再动态加载图片资源。 使用IntersectionObserver API Interse…

jquery 加载

jquery 加载

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

vue加载实现分页

vue加载实现分页

Vue 实现分页加载的方法 使用 Element UI 的分页组件 安装 Element UI 后,可以直接使用其分页组件。在模板中添加分页组件,并绑定相关事件和数据。 <template&g…

vue实现扇形进度

vue实现扇形进度

Vue 实现扇形进度条 使用 SVG 绘制扇形 SVG 的 path 元素可以用于绘制扇形。通过计算扇形的路径数据,结合 Vue 的动态数据绑定实现进度变化。 <template>…