视频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>
相关推荐
两个西柚呀14 分钟前
Vue组件的一些底层细节
前端·javascript·vue.js
paopaokaka_luck14 分钟前
基于SpringBoot+Vue的DIY手工社预约管理系统(Echarts图形化、腾讯地图API)
java·vue.js·人工智能·spring boot·后端·echarts
小白狮ww1 小时前
清华联合字节推出 HuMo,实现三模态协同生成人物视频
人工智能·深度学习·机器学习·音视频·视频生成·多模态模型·人物视频
Q2706810201 小时前
如何才能提升视频的「听觉质感」
音视频
计算机学姐4 小时前
基于微信小程序的高校班务管理系统【2026最新】
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
哆啦A梦15888 小时前
点击Top切换数据
前端·javascript·vue.js
victory04319 小时前
在音频领域采用mamba模型可行性分析
音视频
Black蜡笔小新9 小时前
视频汇聚平台EasyCVR级联播放偶发失败排查:TCP主动模式下的3秒超时响应差
网络·tcp/ip·音视频
小光学长9 小时前
基于Vue的2025年哈尔滨亚冬会志愿者管理系统5zqg6m36(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
前端·数据库·vue.js
未来之窗软件服务9 小时前
未来之窗昭和仙君(二十八)商业收银开发音频播放——东方仙盟筑基期
microsoft·音视频·仙盟创梦ide·东方仙盟·昭和仙君