视频video鼠标移入移除展示隐藏(自定义控件)

效果图

代码

javascript 复制代码
<template>
  <div class="video-container" @mouseover="showControls" @mouseleave="hideControlsAfterDelay">
    <video
      ref="video"
      @loadedmetadata="initializePlayer"
      @timeupdate="updateProgress"
      @ended="resetPlayer"
      width="640"
      height="360"
      src="./11.mp4"
    ></video>
    <!--video自带的属性设置controls="false" 禁用自带默认控件(直接不写才会展示自定义的控件)-->
    <div v-if="isControlsVisible" class="controls">
      <button @click="playPause">{{ isPlaying ? 'Pause' : 'Play' }}</button>
      <input type="range" min="0" :max="duration" v-model.number="currentTime" @input="seekTo" />
      <span>{{ formatTime(currentTime) }} / {{ formatTime(duration) }}</span>
      <button @click="muteUnmute">{{ isMuted ? 'Unmute' : 'Mute' }}</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isControlsVisible: false,
      isPlaying: false,
      isMuted: false,
      currentTime: 0,
      duration: 0,
      controlsTimeout: null,
    };
  },
  methods: {
    playPause() {
      const video = this.$refs.video;
      if (this.isPlaying) {
        video.pause();
      } else {
        video.play();
      }
      this.isPlaying = !this.isPlaying;
    },
    seekTo(event) {
      const video = this.$refs.video;
      video.currentTime = event.target.value;
    },
    updateProgress() {
      this.currentTime = this.$refs.video.currentTime;
    },
    initializePlayer() {
      this.duration = this.$refs.video.duration;
    },
    resetPlayer() {
      this.currentTime = 0;
      this.isPlaying = false;
    },
    muteUnmute() {
      const video = this.$refs.video;
      video.muted = !video.muted;
       this.isMuted = video.muted; // 更新静音状态
    },
    // 可以添加一个计算属性来显示当前的播放/暂停和静音/取消静音文本
    computed: {
      isMuted() {
        return this.$refs.video.muted;
      },
    },
    showControls() {
      this.isControlsVisible = true;
      if (this.controlsTimeout) {
        clearTimeout(this.controlsTimeout);
        this.controlsTimeout = null;
      }
    },
    hideControlsAfterDelay() {
      this.controlsTimeout = setTimeout(() => {
        this.isControlsVisible = false;
      }, 3000); // 3秒后隐藏控件,时间可根据需要调整
    },
    formatTime(time) {
      const minutes = Math.floor(time / 60);
      const seconds = Math.floor(time % 60).toString().padStart(2, '0');
      return `${minutes}:${seconds}`;
    },
  },
};
</script>

<style scoped>
.video-container {
  position: relative;
  width: 640px;
  height: 360px;
  border: 1px solid #ccc;
}

.controls {
  position: absolute;
  bottom: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(255, 255, 255, 0.7);
  padding: 5px;
  border-radius: 5px;
}

.controls button,
.controls input[type="range"] {
  flex: 1;
}

.controls input[type="range"] {
  margin: 0 10px;
}
</style>
相关推荐
王江奎2 小时前
FFmpeg 中编译和使用 soxr 重采样引擎
ffmpeg·音视频
英俊潇洒美少年3 小时前
Vue3 深入响应式系统
前端·javascript·vue.js
枳实-叶3 小时前
基于 ALSA 实现录音保存为 WAV 文件
音视频
优选资源分享4 小时前
小丸工具箱 vR236|ffmpeg 图形化视频压制工具
ffmpeg·音视频
angerdream8 小时前
最新版vue3+TypeScript开发入门到实战教程之路由详解三
前端·javascript·vue.js
localbob10 小时前
Moon VR Video Player中文版下载地址及使用教程:支持8K/12K+多音轨外挂字幕 Moon VR Video Player中文版、Moon VR播放器下载、VR视频播放器推荐、Ste
音视频·vr·moonvr下载·moonvr安装包·moonvr播放器下载·moonvr播放器中文版·moonvr apk
数据潜水员10 小时前
三层统计最小力度的四种方法
javascript·vue.js
潜创微科技--高清音视频芯片方案开发10 小时前
2026年USB转网口方案商趋势洞察--从技术到场景的适配选择
音视频·硬件工程
英俊潇洒美少年11 小时前
Vue3 的 JSX 函数组件,每次更新都会重新运行吗?
前端·javascript·vue.js
Irene199113 小时前
Vue3 响应式系统核心对比:effect, track, trigger,computed, watch, watchEffect
vue.js