mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/vue/learning-system-mobile.git
synced 2025-12-06 17:36:45 +08:00
241 lines
7.7 KiB
Vue
241 lines
7.7 KiB
Vue
/**
|
||
$emit: updateProgress, -> 调整进度条后触发的事件,传递参数:currentProgress
|
||
getMouseDownStatus, -> 获取是否鼠标按下的状态,传递参数:true/false
|
||
props: width, height,
|
||
currentProgress, -> 当前进度,范围0-1
|
||
*/
|
||
<template>
|
||
<div
|
||
class="progress-bar"
|
||
:style="{width: width}"
|
||
@touchstart.left="start"
|
||
@touchmove="move"
|
||
@touchend="up"
|
||
>
|
||
<div class="progress-full" :style="{height: height}">
|
||
<div class="progress-current" :style="{width: currentProgress + '%'}"></div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script>
|
||
export default {
|
||
name: "progress-bar",
|
||
props: {
|
||
width: {
|
||
type: String,
|
||
default: "100%",
|
||
},
|
||
height: {
|
||
type: String,
|
||
default: "4px",
|
||
},
|
||
fullScreenFlag:{
|
||
type: Boolean,
|
||
default: false,
|
||
},
|
||
// 当前进度,范围0-1
|
||
currentProgress: {
|
||
type: Number,
|
||
default: 0,
|
||
},
|
||
isDrag:{
|
||
type: Boolean,
|
||
default: true,
|
||
},
|
||
blobId:{
|
||
type: String,
|
||
}
|
||
},
|
||
data() {
|
||
return {
|
||
is_mousedown_progress: false, //是否点击了进度条并保持点击状态
|
||
current_width_px: 0, // 当前竖屏进度条的像素长度
|
||
current_height_px: 0, // 当前全屏进度条的像素长度
|
||
init_clientX: 0, //竖屏初始进度的偏移量,相对于视口
|
||
init_clientY: 0, //全屏初始进度的偏移量,相对于视口
|
||
dom_full: null, //完整进度条的dom,即进度条背景
|
||
};
|
||
},
|
||
mounted() {
|
||
//console.log(this.isDrag,'isDrag');
|
||
//初始化一些固定数据
|
||
let dom_full = this.$el.getElementsByClassName("progress-full")[0];
|
||
this.dom_full = dom_full;
|
||
// window.addEventListener("touchstart", (e) => {
|
||
// console.log("touchstart::",this.is_mousedown_progress)
|
||
// //if (this.is_mousedown_progress) {
|
||
// this.down(e);
|
||
// //}
|
||
// });
|
||
//绑定全局监听器
|
||
window.addEventListener("touchmove", (e) => {
|
||
//console.log("touchmove::",this.is_mousedown_progress)
|
||
if (this.is_mousedown_progress) {
|
||
this.move(e);
|
||
e.preventDefault();
|
||
}
|
||
},{ passive: false });
|
||
window.addEventListener("touchend", (e) => {
|
||
//console.log("touchend::",this.is_mousedown_progress)
|
||
this.up(e);
|
||
// e.preventDefault();
|
||
});
|
||
},
|
||
beforeDestroy() {
|
||
//
|
||
},
|
||
methods: {
|
||
start(e) {
|
||
this.$emit("getMouseDownStatus", true);
|
||
this.is_mousedown_progress = true;
|
||
if(this.fullScreenFlag){
|
||
// 全屏
|
||
// 获取完整进度条的clientY(dom左上角)
|
||
let init_clientY = this.dom_full.getBoundingClientRect().top;
|
||
// 计算调整后的当前进度条的长度
|
||
this.current_height_px = e.touches[0].clientY - init_clientY;
|
||
// 设置当前的播放进度(同时作用于当前进度条的样式)
|
||
let current = (e.touches[0].clientY - init_clientY) / this.dom_full.clientWidth;
|
||
if(current>1) current = 1;
|
||
if(current<0) current = 0;
|
||
var time = localStorage.getItem('videoProgressData');
|
||
var arr = time&&JSON.parse(time) || {}
|
||
//console.log('start::',e,this.isDrag,this.fullScreenFlag,time,arr[this.blobId] ,current,e.touches[0].clientY,init_clientY,this.dom_full.clientHeight,this.current_height_px,this.dom_full)
|
||
// 禁止拖动
|
||
if(!this.isDrag && time && arr[this.blobId] < current) return;
|
||
|
||
this.$emit("updateProgress",'start', current);
|
||
}else{
|
||
// 横屏
|
||
// 获取完整进度条的clientX(dom左上角)
|
||
let init_clientX = this.dom_full.getBoundingClientRect().left;
|
||
// 计算调整后的当前进度条的长度
|
||
this.current_width_px = e.touches[0].clientX - init_clientX;
|
||
// 设置当前的播放进度(同时作用于当前进度条的样式)
|
||
let current = (e.touches[0].clientX - init_clientX) / this.dom_full.clientWidth;
|
||
if(current>1) current = 1;
|
||
if(current<0) current = 0;
|
||
var time = localStorage.getItem('videoProgressData');
|
||
var arr = time&&JSON.parse(time) || {}
|
||
//console.log('start::',e,this.isDrag,this.fullScreenFlag,time,arr[this.blobId] ,current,e.touches[0].clientX,init_clientX,this.dom_full.clientWidth)
|
||
// 禁止拖动
|
||
if(!this.isDrag && time && arr[this.blobId] < current) return;
|
||
|
||
this.$emit("updateProgress",'start', current);
|
||
}
|
||
},
|
||
move(e) {
|
||
if (this.is_mousedown_progress) {
|
||
if(this.fullScreenFlag){
|
||
// 全屏
|
||
let init_clientY = this.dom_full.getBoundingClientRect().top;
|
||
this.current_height_px = e.touches[0].clientY - init_clientY;
|
||
let current = (e.touches[0].clientY - init_clientY) / this.dom_full.clientWidth;
|
||
if(current>1) current = 1;
|
||
if(current<0) current = 0;
|
||
var time = localStorage.getItem('videoProgressData');
|
||
var arr = time&&JSON.parse(time) || {}
|
||
//console.log('move::',e,this.isDrag,this.fullScreenFlag,time,arr[this.blobId] ,current,this.dom_full)
|
||
// 禁止拖动
|
||
if(!this.isDrag && time && arr[this.blobId] < current) return;
|
||
this.$emit("updateProgress",'move', current);
|
||
}else{
|
||
// 横屏
|
||
let init_clientX = this.dom_full.getBoundingClientRect().left;
|
||
this.current_width_px = e.touches[0].clientX - init_clientX;
|
||
let current = (e.touches[0].clientX - init_clientX) / this.dom_full.clientWidth;
|
||
if(current>1) current = 1;
|
||
if(current<0) current = 0;
|
||
var time = localStorage.getItem('videoProgressData');
|
||
var arr = time&&JSON.parse(time) || {}
|
||
//console.log('move::',e,this.isDrag,this.fullScreenFlag,time,arr[this.blobId] ,current)
|
||
// 禁止拖动
|
||
if(!this.isDrag && time && arr[this.blobId] < current) return;
|
||
this.$emit("updateProgress",'move', current);
|
||
}
|
||
}
|
||
},
|
||
up() {
|
||
if (this.is_mousedown_progress) {
|
||
// 标记鼠标不处于按下的状态了
|
||
this.is_mousedown_progress = false;
|
||
this.$emit("getMouseDownStatus", false);
|
||
if(this.fullScreenFlag){
|
||
// 全屏
|
||
// 松开鼠标后,即调整进度条后,此时的进度(0-1)
|
||
let current = this.current_height_px / this.dom_full.clientWidth;
|
||
if(current>1) current = 1;
|
||
if(current<0) current = 0;
|
||
var time = localStorage.getItem('videoProgressData');
|
||
var arr = time&&JSON.parse(time) || {}
|
||
//console.log('up::',this.isDrag,this.fullScreenFlag,time,arr[this.blobId] ,current,this.dom_full)
|
||
// 禁止拖动
|
||
if(!this.isDrag && time && arr[this.blobId] < current) return;
|
||
this.$emit("updateProgress",'end', current);
|
||
}else{
|
||
// 横屏
|
||
// 松开鼠标后,即调整进度条后,此时的进度(0-1)
|
||
let current = this.current_width_px / this.dom_full.clientWidth;
|
||
if(current>1) current = 1;
|
||
if(current<0) current = 0;
|
||
var time = localStorage.getItem('videoProgressData');
|
||
var arr = time&&JSON.parse(time) || {}
|
||
//console.log('up::',this.isDrag,this.fullScreenFlag,time,arr[this.blobId] ,current)
|
||
// 禁止拖动
|
||
if(!this.isDrag && time && arr[this.blobId] < current) return;
|
||
this.$emit("updateProgress",'end', current);
|
||
}
|
||
}
|
||
},
|
||
},
|
||
};
|
||
</script>
|
||
|
||
<style scoped>
|
||
.progress-bar {
|
||
position: relative;
|
||
cursor: pointer;
|
||
padding: 2px 0;
|
||
margin: 0;
|
||
transition: height 0.2s;
|
||
/*overflow: hidden;*/
|
||
}
|
||
.progress-bar:hover .progress-full {
|
||
height: 2px !important;
|
||
}
|
||
.progress-full {
|
||
position: relative;
|
||
display: inline-block;
|
||
width: 100%;
|
||
background: rgb(201, 201, 201);
|
||
transition: height 0.3s;
|
||
}
|
||
.progress-current {
|
||
position: absolute;
|
||
left: 0;
|
||
top: 0;
|
||
height: inherit;
|
||
width: 0%;
|
||
display: inline-block;
|
||
background-color: var(--primaryColor);
|
||
}
|
||
.progress-current::after {
|
||
content: "";
|
||
position: absolute;
|
||
display: inline-block;
|
||
right: 0;
|
||
top: 50%;
|
||
bottom: 50%;
|
||
width: 12px;
|
||
height: 12px;
|
||
border-radius: 100%;
|
||
background: #fff;
|
||
transform: translate(50%, -50%);
|
||
opacity: 1;
|
||
transition: opacity 0.3s;
|
||
}
|
||
.progress-bar:hover .progress-current::after {
|
||
opacity: 1;
|
||
}
|
||
</style> |