当前位置:首页 > 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实现driver

vue实现driver

Vue 实现 Driver.js 引导功能 Driver.js 是一个轻量级的 JavaScript 库,用于在网页上创建引导式导览。以下是在 Vue 项目中集成 Driver.js 的详细方法:…

vue实现slidetoggle

vue实现slidetoggle

Vue 实现 SlideToggle 效果 SlideToggle 是一种常见的交互效果,元素以滑动方式展开或收起。以下是几种实现方法: 使用 CSS Transition 和 v-show…

vue实现加减

vue实现加减

Vue 实现加减功能 在 Vue 中实现加减功能可以通过数据绑定和事件监听来完成。以下是一个简单的实现方法: 模板部分 <template> <div> <…

vue实现框选

vue实现框选

Vue 实现框选功能 在 Vue 中实现框选功能通常需要监听鼠标事件,计算选区范围,并根据选区范围高亮或选中元素。以下是实现框选功能的关键步骤。 监听鼠标事件 在 Vue 模板中,为容器元素绑定鼠标…

vue实现计数

vue实现计数

Vue 实现计数功能 在 Vue 中实现计数功能可以通过多种方式完成,以下是几种常见的方法: 使用 Vue 数据绑定和事件监听 通过 Vue 的数据绑定和事件监听,可以轻松实现计数功能。以下是一个简…

vue框架实现CRM

vue框架实现CRM

Vue框架实现CRM系统 使用Vue框架构建CRM(客户关系管理)系统需要结合前端技术栈和后端API。以下为关键实现方法: 技术选型 核心框架:Vue 3(Composition API) UI组件…