当前位置:首页 > VUE

vue实现视频播放条

2026-01-20 07:54:34VUE

Vue 实现视频播放进度条

在 Vue 中实现视频播放进度条需要结合 HTML5 的 <video> 元素和 Vue 的数据绑定功能。以下是一个完整的实现方案。

基础结构

创建一个 Vue 组件,包含视频元素和进度条控件。

<template>
  <div class="video-player">
    <video 
      ref="videoPlayer"
      @timeupdate="updateProgress"
      @loadedmetadata="setDuration"
    >
      <source :src="videoSrc" type="video/mp4">
    </video>

    <div class="controls">
      <input 
        type="range" 
        v-model="progress"
        @input="seekVideo"
        min="0"
        :max="duration"
      >
    </div>
  </div>
</template>

核心逻辑

在 Vue 的 script 部分实现播放控制逻辑。

<script>
export default {
  data() {
    return {
      videoSrc: 'your-video.mp4',
      progress: 0,
      duration: 0
    }
  },
  methods: {
    updateProgress() {
      this.progress = this.$refs.videoPlayer.currentTime;
    },
    seekVideo() {
      this.$refs.videoPlayer.currentTime = this.progress;
    },
    setDuration() {
      this.duration = this.$refs.videoPlayer.duration;
    }
  }
}
</script>

样式优化

添加基础样式提升用户体验。

<style scoped>
.video-player {
  position: relative;
  width: 100%;
  max-width: 800px;
}

video {
  width: 100%;
}

.controls {
  background: rgba(0,0,0,0.5);
  padding: 10px;
}

input[type="range"] {
  width: 100%;
}
</style>

扩展功能

添加播放/暂停按钮和控制面板。

<div class="controls">
  <button @click="togglePlay">
    {{ isPlaying ? 'Pause' : 'Play' }}
  </button>
  <input 
    type="range" 
    v-model="progress"
    @input="seekVideo"
    min="0"
    :max="duration"
  >
  <span>{{ formatTime(progress) }} / {{ formatTime(duration) }}</span>
</div>
data() {
  return {
    isPlaying: false
  }
},
methods: {
  togglePlay() {
    const video = this.$refs.videoPlayer;
    if (video.paused) {
      video.play();
      this.isPlaying = true;
    } else {
      video.pause();
      this.isPlaying = false;
    }
  },
  formatTime(seconds) {
    const minutes = Math.floor(seconds / 60);
    const secs = Math.floor(seconds % 60);
    return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;
  }
}

注意事项

  • 确保视频文件路径正确
  • 跨浏览器测试视频格式兼容性
  • 移动端需要添加触摸事件支持
  • 考虑添加缓冲进度显示
  • 可以引入第三方库如 video.js 获得更完整功能

这个实现提供了视频播放的核心功能,可以根据需要进一步扩展其他特性如全屏控制、音量调节等。

vue实现视频播放条

标签: 视频播放vue
分享给朋友:

相关文章

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue实现人脸识别比对

vue实现人脸识别比对

Vue 实现人脸识别比对的方法 使用现成的人脸识别 API 通过调用第三方人脸识别 API(如 Face++、百度 AI、腾讯云等)实现比对功能。注册开发者账号获取 API Key 和 Secret,…

vue实现用户信息表

vue实现用户信息表

Vue 实现用户信息表的方法 使用 Vue CLI 创建项目 安装 Vue CLI 并初始化项目: npm install -g @vue/cli vue create user-info-tab…

vue实现选中单元格

vue实现选中单元格

Vue 实现选中单元格的方法 基础实现思路 在 Vue 中实现选中单元格功能,通常可以通过动态绑定 class 或 style 来实现。以下是一个基于表格的简单实现示例: <template…

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue tag实现

vue tag实现

Vue 标签实现方法 在 Vue 中实现标签功能可以通过多种方式完成,常见场景包括动态标签生成、标签输入框、标签管理等。以下是几种典型实现方案: 动态标签列表渲染 使用 v-for 指令渲染标签数组…