视频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>
相关推荐
EMT21 分钟前
在 Vue 项目中使用 URL Query 保存和恢复搜索条件
javascript·vue.js
我是日安29 分钟前
从零到一打造 Vue3 响应式系统 Day 9 - Effect:调度器实现与应用
前端·vue.js
鹏多多1 小时前
深入解析vue的keep-alive缓存机制
前端·javascript·vue.js
用户516816614584117 小时前
Vue Router 路由懒加载引发的生产页面白屏问题
vue.js·vue-router
前端缘梦17 小时前
Vue Keep-Alive 组件详解:优化性能与保留组件状态的终极指南
前端·vue.js·面试
Simon_He18 小时前
这次来点狠的:用 Vue 3 把 AI 的“碎片 Markdown”渲染得又快又稳(Monaco 实时更新 + Mermaid 渐进绘图)
前端·vue.js·markdown
王同学QaQ1 天前
Vue3对接UE,通过MQTT完成通讯
javascript·vue.js
华仔啊1 天前
基于 RuoYi-Vue 轻松实现单用户登录功能,亲测有效
java·vue.js·后端
艾小码1 天前
告别Vue混入的坑!Composition API让我效率翻倍的3个秘密
前端·javascript·vue.js
Gracemark2 天前
高德地图-地图选择经纬度问题【使用输入提示-使用Autocomplete进行联想输入】(复盘)
vue.js