当前位置:首页 > 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 会将其转换为异步加载的组件。适用于路由或组件按需加载。 const AsyncComponent…

vue实现页面加载

vue实现页面加载

Vue 实现页面加载的方法 在 Vue 中实现页面加载效果可以通过多种方式完成,以下是几种常见的方法: 使用 v-if 或 v-show 控制加载状态 通过绑定一个布尔值来控制加载动画的显示与隐藏。…

vue加载更多实现

vue加载更多实现

滚动监听实现加载更多 通过监听滚动事件判断是否到达底部触发加载。在组件的mounted钩子中添加滚动事件监听器,计算滚动位置与文档高度的关系。 mounted() { window.addEve…

vue如何实现异步加载

vue如何实现异步加载

Vue异步加载的实现方式 动态导入组件 使用import()语法动态导入组件,Vue会将其识别为异步组件。这种方式适用于路由懒加载或按需加载组件。 const AsyncComponent = ()…

vue图片实现懒加载

vue图片实现懒加载

vue图片实现懒加载的方法 使用Intersection Observer API Intersection Observer API是现代浏览器提供的原生API,可以高效检测元素是否进入视口。 /…

vue怎么实现同步加载

vue怎么实现同步加载

Vue 实现同步加载的方法 在 Vue 中,同步加载通常指在组件渲染前确保数据已加载完成。以下是几种常见的实现方式: 使用 async/await 配合生命周期钩子 在组件的 created 或 m…