当前位置:首页 > VUE

vue实现视频播放条

2026-02-20 23:56:45VUE

实现视频播放条的基本结构

使用 Vue 结合 HTML5 的 <video> 标签实现视频播放条的核心功能。以下是一个基础模板:

<template>
  <div class="video-player">
    <video 
      ref="videoPlayer" 
      @timeupdate="updateProgress" 
      @play="onPlay" 
      @pause="onPause"
    >
      <source :src="videoSrc" type="video/mp4">
    </video>
    <div class="controls">
      <button @click="togglePlay">{{ isPlaying ? 'Pause' : 'Play' }}</button>
      <input 
        type="range" 
        v-model="progress" 
        @input="seekVideo" 
        min="0" 
        max="100"
      >
      <span>{{ currentTimeFormatted }} / {{ durationFormatted }}</span>
    </div>
  </div>
</template>

数据与事件处理

在 Vue 的 datamethods 中定义播放条的核心逻辑:

<script>
export default {
  data() {
    return {
      videoSrc: 'path/to/video.mp4',
      isPlaying: false,
      progress: 0,
      currentTime: 0,
      duration: 0
    };
  },
  computed: {
    currentTimeFormatted() {
      return this.formatTime(this.currentTime);
    },
    durationFormatted() {
      return this.formatTime(this.duration);
    }
  },
  methods: {
    togglePlay() {
      const video = this.$refs.videoPlayer;
      this.isPlaying ? video.pause() : video.play();
    },
    onPlay() {
      this.isPlaying = true;
    },
    onPause() {
      this.isPlaying = false;
    },
    updateProgress() {
      const video = this.$refs.videoPlayer;
      this.currentTime = video.currentTime;
      this.duration = video.duration;
      this.progress = (video.currentTime / video.duration) * 100;
    },
    seekVideo(e) {
      const video = this.$refs.videoPlayer;
      const seekTime = (e.target.value / 100) * video.duration;
      video.currentTime = seekTime;
    },
    formatTime(seconds) {
      const mins = Math.floor(seconds / 60);
      const secs = Math.floor(seconds % 60);
      return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
    }
  }
};
</script>

样式优化

通过 CSS 美化播放条,确保交互友好:

<style scoped>
.video-player {
  max-width: 800px;
  margin: 0 auto;
  position: relative;
}
video {
  width: 100%;
  display: block;
}
.controls {
  background: #333;
  padding: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
}
input[type="range"] {
  flex-grow: 1;
}
button {
  background: #fff;
  border: none;
  padding: 5px 10px;
  cursor: pointer;
}
</style>

扩展功能

  1. 音量控制
    添加音量滑块,通过 video.volume 属性调整:

    <input 
      type="range" 
      v-model="volume" 
      @input="setVolume" 
      min="0" 
      max="1" 
      step="0.1"
    >
    setVolume(e) {
      this.$refs.videoPlayer.volume = e.target.value;
    }
  2. 全屏切换
    使用浏览器全屏 API:

    toggleFullscreen() {
      const video = this.$refs.videoPlayer;
      video.requestFullscreen?.call(video);
    }
  3. 快捷键支持
    监听键盘事件实现空格键播放/暂停:

    vue实现视频播放条

    mounted() {
      document.addEventListener('keydown', (e) => {
        if (e.code === 'Space') this.togglePlay();
      });
    }

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

相关文章

vue弹幕实现不重叠

vue弹幕实现不重叠

实现 Vue 弹幕不重叠的方法 CSS 定位与动画控制 通过绝对定位和动态计算弹幕轨道高度,确保弹幕在不同轨道上运行。每条弹幕占据固定高度的轨道,避免重叠。 // 弹幕轨道高度计算 const tr…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="we…

vue实现 页面

vue实现 页面

Vue 实现页面的核心方法 创建 Vue 项目 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 是官方脚手架工具,适合传统项目;Vite 提供更快的现代构建体验。 npm cre…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…

vue原生实现

vue原生实现

Vue 原生实现方法 在 Vue 中实现原生功能通常指直接使用 Vue 的核心特性(如指令、响应式数据、组件等)而非依赖第三方库。以下是几种常见场景的原生实现方式: 数据绑定与响应式更新 Vue 的…

用vue实现echarts

用vue实现echarts

使用 Vue 实现 ECharts 安装依赖 在 Vue 项目中安装 ECharts 和 Vue-ECharts(官方推荐的 Vue 封装库): npm install echarts vue-ec…