视频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>
相关推荐
前端无涯17 分钟前
React/Vue 代理配置全攻略:Vite 与 Webpack 实战指南
vue.js·react.js
JIngJaneIL1 小时前
基于java+ vue交友系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·交友
苹果酱05672 小时前
解决linux mysql命令 bash: mysql: command not found 的方法
java·vue.js·spring boot·mysql·课程设计
拉不动的猪2 小时前
回顾计算属性的缓存与监听的触发返回结果
前端·javascript·vue.js
山海青风2 小时前
藏文TTS介绍:6 MMS 项目的多语言 TTS
人工智能·python·神经网络·音视频
小七不懂前端2 小时前
我用 NestJS + Vue3 + Prisma + PostgreSQL 打造了一个企业级 sass 多租户平台
前端·vue.js·后端
进击的野人3 小时前
一个基于 Vue 的 GitHub 用户搜索案例
前端·vue.js·前端框架
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue职位管理推荐系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
前端码农一枚4 小时前
Vue3+TypeScript实现手机扫码功能
vue.js·typescript
钱多多8105 小时前
Vue版本降级操作指南(解决依赖冲突与版本不一致问题)
前端·javascript·vue.js·前端框架