vue2封装一个视频显示组件

时间: 2023-12-28 20:05:47 浏览: 31
以下是一个简单的Vue2视频显示组件的封装示例: ```vue <template> <div class="video-container"> <video ref="videoPlayer" :src="source" @play="onPlay" @pause="onPause" @ended="onEnded"></video> <div class="controls" :class="{ active: showControls }"> <button @click="togglePlay"><i :class="playIcon"></i></button> <div class="progress"> <div class="bar" :style="{ width: progress + '%' }"></div> <div class="thumb" :style="{ left: progress + '%' }" @mousedown="onThumbMouseDown"></div> </div> <span class="current-time">{{ currentTime }}</span> <span class="total-time">{{ totalTime }}</span> <button @click="toggleMute"><i :class="muteIcon"></i></button> <div class="volume"> <div class="bar" :style="{ width: volume + '%' }"></div> <div class="thumb" :style="{ left: volume + '%' }" @mousedown="onThumbMouseDown"></div> </div> <button @click="toggleFullscreen"><i :class="fullscreenIcon"></i></button> </div> </div> </template> <script> export default { props: { source: { type: String, required: true }, autoplay: { type: Boolean, default: false }, controls: { type: Boolean, default: true }, loop: { type: Boolean, default: false }, muted: { type: Boolean, default: false }, poster: { type: String, default: '' } }, data() { return { showControls: false, isPlaying: false, isMuted: false, isFullscreen: false, duration: 0, currentTime: 0, volume: 100, progress: 0 } }, mounted() { const videoPlayer = this.$refs.videoPlayer videoPlayer.addEventListener('durationchange', this.onDurationChange) videoPlayer.addEventListener('timeupdate', this.onTimeUpdate) videoPlayer.addEventListener('volumechange', this.onVolumeChange) if (this.autoplay) { videoPlayer.play() this.isPlaying = true } if (this.muted) { videoPlayer.muted = true this.isMuted = true } if (this.controls) { this.showControls = true } if (this.poster) { videoPlayer.poster = this.poster } }, beforeUnmount() { const videoPlayer = this.$refs.videoPlayer videoPlayer.removeEventListener('durationchange', this.onDurationChange) videoPlayer.removeEventListener('timeupdate', this.onTimeUpdate) videoPlayer.removeEventListener('volumechange', this.onVolumeChange) }, computed: { playIcon() { return this.isPlaying ? 'fa fa-pause' : 'fa fa-play' }, muteIcon() { return this.isMuted ? 'fa fa-volume-off' : 'fa fa-volume-up' }, fullscreenIcon() { return this.isFullscreen ? 'fa fa-compress' : 'fa fa-expand' }, totalTime() { const totalSeconds = parseInt(this.duration) const minutes = Math.floor(totalSeconds / 60) const seconds = totalSeconds - minutes * 60 return `${minutes}:${seconds.toString().padStart(2, '0')}` } }, methods: { onDurationChange() { this.duration = this.$refs.videoPlayer.duration }, onTimeUpdate() { this.currentTime = this.$refs.videoPlayer.currentTime this.progress = (this.currentTime / this.duration) * 100 }, onVolumeChange() { this.volume = Math.round(this.$refs.videoPlayer.volume * 100) }, onThumbMouseDown(event) { const thumb = event.target const bar = thumb.previousSibling const startX = event.clientX const onMouseMove = event => { const delta = event.clientX - startX const rect = bar.getBoundingClientRect() const barWidth = rect.right - rect.left const thumbWidth = thumb.offsetWidth const left = Math.min(Math.max(rect.left, rect.left + delta), rect.right - thumbWidth) const position = (left - rect.left) / barWidth thumb.style.left = position * 100 + '%' bar.style.width = position * 100 + '%' if (bar === thumb.nextSibling) { this.$refs.videoPlayer.volume = position } else { this.$refs.videoPlayer.currentTime = position * this.duration } } const onMouseUp = () => { document.removeEventListener('mousemove', onMouseMove) document.removeEventListener('mouseup', onMouseUp) } document.addEventListener('mousemove', onMouseMove) document.addEventListener('mouseup', onMouseUp) }, onPlay() { this.isPlaying = true }, onPause() { this.isPlaying = false }, onEnded() { if (this.loop) { this.$refs.videoPlayer.currentTime = 0 this.$refs.videoPlayer.play() } else { this.isPlaying = false } }, togglePlay() { const videoPlayer = this.$refs.videoPlayer if (this.isPlaying) { videoPlayer.pause() } else { videoPlayer.play() } this.isPlaying = !this.isPlaying }, toggleMute() { const videoPlayer = this.$refs.videoPlayer if (this.isMuted) { videoPlayer.muted = false videoPlayer.volume = 0.5 } else { videoPlayer.muted = true } this.isMuted = !this.isMuted }, toggleFullscreen() { const videoContainer = this.$el if (this.isFullscreen) { if (document.exitFullscreen) { document.exitFullscreen() } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen() } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen() } else if (document.msExitFullscreen) { document.msExitFullscreen() } } else { if (videoContainer.requestFullscreen) { videoContainer.requestFullscreen() } else if (videoContainer.webkitRequestFullscreen) { videoContainer.webkitRequestFullscreen() } else if (videoContainer.mozRequestFullScreen) { videoContainer.mozRequestFullScreen() } else if (videoContainer.msRequestFullscreen) { videoContainer.msRequestFullscreen() } } this.isFullscreen = !this.isFullscreen } } } </script> <style> .video-container { position: relative; width: 100%; height: 0; padding-bottom: 56.25%; overflow: hidden; } .video-container video { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: contain; } .controls { position: absolute; bottom: 0; left: 0; width: 100%; padding: 10px; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 14px; text-align: center; transition: opacity 0.3s; opacity: 0; pointer-events: none; } .controls.active { opacity: 1; pointer-events: auto; } .controls button { background-color: transparent; border: none; outline: none; cursor: pointer; padding: 0; margin-right: 10px; } .controls button i { font-size: 24px; } .progress { position: relative; height: 5px; width: 100%; background-color: rgba(255, 255, 255, 0.5); margin: 10px 0; cursor: pointer; } .progress .bar { position: absolute; top: 0; left: 0; height: 100%; background-color: #fff; } .progress .thumb { position: absolute; top: -7.5px; left: 0; width: 15px; height: 15px; border-radius: 50%; background-color: #fff; cursor: pointer; transform: translateX(-50%); } .volume { position: relative; height: 5px; width: 60px; background-color: rgba(255, 255, 255, 0.5); margin-right: 10px; cursor: pointer; } .volume .bar { position: absolute; top: 0; left: 0; height: 100%; background-color: #fff; } .volume .thumb { position: absolute; top: -7.5px; left: 0; width: 15px; height: 15px; border-radius: 50%; background-color: #fff; cursor: pointer; transform: translateX(-50%); } </style> ``` 使用示例: ```vue <template> <div> <video-player source="https://www.w3schools.com/html/mov_bbb.mp4" poster="https://www.w3schools.com/html/pic_trulli.jpg"></video-player> </div> </template> <script> import VideoPlayer from './VideoPlayer.vue' export default { components: { VideoPlayer } } </script> ```

相关推荐

最新推荐

recommend-type

vue element-ui之怎么封装一个自己的组件的详解

主要介绍了vue element-ui之怎么封装一个自己的组件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

使用Vue组件实现一个简单弹窗效果

主要介绍了使用Vue组件实现一个简单弹窗效果,本文主要内容会涉及到弹窗遮罩的实现, slot 插槽的使用方式,props 、 $emit 传参,具体组件代码也传上去了。需要的朋友可以参考下
recommend-type

vue之将echart封装为组件

主要介绍了vue之将echart封装为组件,本文将会以雷达图为案例,一步步讲解在vue项目中如何使用echart,如何将其封装为能重复调用的组件
recommend-type

vue实现图片预览组件封装与使用

主要为大家详细介绍了vue实现图片预览组件封装与使用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

vue多级多选菜单组件开发

主要为大家分享了vue多级多选菜单组件开发案例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。