当前位置:首页 > 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动画和Vue的数据绑定结合完成。以下是一个完整的实现方案: 准备工作 需要安装Vue.js环境,可以通过CDN引入或使用Vue C…

vue树形实现

vue树形实现

Vue 树形组件实现方法 使用递归组件实现树形结构 递归组件是 Vue 中实现树形结构的常见方法。通过组件调用自身的方式,可以轻松构建多层嵌套的树形结构。 <template> &l…

vue实现input

vue实现input

Vue 实现 Input 输入框的方法 基本双向绑定 使用 v-model 指令实现双向数据绑定,适用于大多数表单场景: <template> <input v-model="…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue scrolltop 实现

vue scrolltop 实现

实现 Vue 中的 scrollTop 在 Vue 中实现滚动到顶部功能可以通过多种方式完成,包括使用原生 JavaScript、Vue 指令或第三方库。以下是几种常见的实现方法: 使用原生 Jav…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…