当前位置:首页 > 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 中定义播放条的核心逻辑:

vue实现视频播放条

<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 属性调整:

    vue实现视频播放条

    <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. 快捷键支持
    监听键盘事件实现空格键播放/暂停:

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

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

相关文章

代码实现vue

代码实现vue

创建Vue项目 使用Vue CLI快速初始化项目,确保已安装Node.js和npm/yarn: npm install -g @vue/cli vue create my-vue-project…

vue实现alert

vue实现alert

实现自定义 Alert 组件 在 Vue 中可以通过组件化方式实现自定义 Alert 弹窗。以下是基于 Vue 3 的实现示例: 组件代码 (Alert.vue) <template>…

vue实现$.extend

vue实现$.extend

Vue 实现类似 jQuery 的 $.extend 功能 在 Vue 中实现类似 jQuery 的 $.extend 功能,可以通过多种方式完成。$.extend 主要用于合并多个对象的属性,Vue…

vue 全景实现

vue 全景实现

Vue 全景实现方案 在Vue中实现全景效果,通常需要结合WebGL或第三方库来处理3D场景渲染。以下是几种常见方法: 使用Three.js库 Three.js是一个强大的WebGL库,适合创建3D…

vue实现webapp

vue实现webapp

Vue 实现 WebApp 的基本步骤 环境准备 确保已安装 Node.js 和 npm/yarn。通过 Vue CLI 或 Vite 创建项目: npm install -g @vue/cli…

vue 实现搜索

vue 实现搜索

实现 Vue 搜索功能 在 Vue 中实现搜索功能通常涉及以下几个关键步骤: 数据绑定与输入监听 使用 v-model 双向绑定搜索输入框的值,监听用户输入: <template>…