视频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>
相关推荐
程序员杨工3 小时前
【原创】SpringBoot3+Vue3客户管理系统
vue.js·springboot
B站_计算机毕业设计之家3 小时前
基于大数据的短视频数据分析系统 Spark哔哩哔哩视频数据分析可视化系统 Hadoop大数据技术 情感分析 舆情分析 爬虫 推荐系统 协同过滤推荐算法 ✅
大数据·hadoop·爬虫·spark·音视频·短视频·1024程序员节
theOtherSky5 小时前
element+vue3 table上下左右键切换input和select
javascript·vue.js·elementui·1024程序员节
Pu_Nine_96 小时前
Vue 3 + TypeScript 项目性能优化全链路实战:从 2.1MB 到 130KB 的蜕变
前端·vue.js·性能优化·typescript·1024程序员节
一个处女座的程序猿O(∩_∩)O6 小时前
Vue CLI 插件开发完全指南:从原理到实战
前端·javascript·vue.js
姜至7 小时前
el-calendar实现自定义展示效果
前端·vue.js
音视频牛哥7 小时前
低空经济的实时神经系统:空地一体化音视频架构的技术演进
机器学习·计算机视觉·音视频·低空经济·人工智能+·evtol·ai感知网络
拉不动的猪7 小时前
webpack分包优化简单分析
前端·vue.js·webpack
麦麦大数据8 小时前
F035 vue+neo4j中医南药药膳知识图谱可视化系统 | vue+flask
vue.js·知识图谱·neo4j·中医·中药·药膳·南药