参考文章: 链接
- pc
<!DOCTYPE>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>进度条</title>
<style>
/* 整体样式,消除默认样式 */
body{
margin:0;
padding:0;
}
#box{
position:relative;
width:1000px;
height:30px;
margin:100px auto;
}
#progress{
position:relative;
width:900px;
height:30px;
background:#999999;
border-radius:8px;
margin:0 auto;
}
#progress_head{
width:0px;
height:100%;
border-top-left-radius:8px;
border-bottom-left-radius:8px;
background:#9933CC;
}
#span{
position:absolute;
width:20px;
height:38px;
background:#9933CC;
top:-4px;
left:0px;
cursor:pointer;
}
/* #percentage{
position:absolute;
line-height:30px;
text-align:center;
right:-44px;
top:0;
} */
</style>
</head>
<body>
<!-- 整体盒子 -->
<div id="box">
<!-- 进度条整体 -->
<div id="progress">
<!-- 进度条长度 -->
<div id="progress_head"></div>
<!-- 进度条移动条 -->
<span id="span"></span>
<div>
<!-- 显示数据 -->
<!-- <div id="percentage">0%</div> -->
</div>
</body>
</html>
<script>
//js获取节点
var oProgress=document.getElementById('progress');
var oProgress_head=document.getElementById('progress_head');
var oSpan=document.getElementById('span');
// var oPercentage=document.getElementById('percentage')
//添加事件 鼠标按下的事件
oSpan.onmousedown=function(event){
var event=event || window.event;
console.log(event)
// clientX: 相对文档的水平坐标
// clientY: 相对文档的垂直坐标
// offsetLeft值跟offsetTop值的获取跟父级元素没关系,而是跟其上一级的定位元素(除position:static;外的所有定位如fixed,relative,absolute)有关系。
// offsetLeft: 对象相对于版面或由 offsetParent 左侧位置
// offsetTop:对象相对于版面或由 offsetTop 顶端位置
var x=event.clientX-oSpan.offsetLeft;
console.log('x', x)
// 监听鼠标移动
document.onmousemove=function(){
var event=event || window.event;
var wX=event.clientX-x;
console.log('wX 元素的实时坐标', wX)
console.log(oProgress.offsetWidth) // 900 进度条整体的宽度
if(wX<0)
{
wX=0;
}else if(wX>=oProgress.offsetWidth-20)
{
wX=oProgress.offsetWidth - 20;
}
oProgress_head.style.width=wX+'px';
oSpan.style.left=wX+'px';
// oPercentage.innerHTML=parseInt(wX/(oProgress.offsetWidth-20)*100)+'%';
return false;
};
document.onmouseup=function(){
document.onmousemove=null;
};
};
</script>
- 移动端
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
html,body {
height: 100%;
}
body{
margin:0;
padding:0;
}
#box{
position:relative;
width:300px;
height:30px;
margin:100px auto;
}
#progress{
position:relative;
width:200px;
height:30px;
background:#999999;
border-radius:8px;
margin:0 auto;
}
#progress_head{
width:0px;
height:100%;
border-top-left-radius:8px;
border-bottom-left-radius:8px;
background:#9933CC;
}
#span{
position:absolute;
width:20px;
height:38px;
background:#9933CC;
top:-4px;
left:0px;
cursor:pointer;
}
</style>
</head>
<body>
<div id="box">
<!-- 进度条整体 -->
<div id="progress">
<!-- 进度条长度 -->
<div id="progress_head"></div>
<!-- 进度条移动条 -->
<span id="span"></span>
<div>
</div>
<script>
//js获取节点
var oProgress=document.getElementById('progress'); // 进度条整体的宽度
var oProgress_head=document.getElementById('progress_head'); // 左侧覆盖区域
var oSpan=document.getElementById('span'); // 进度条滑块
var oProgressWidth = Number(oProgress.clientWidth)
var oSpanWidth = Number(oSpan.clientWidth)
var width = oProgressWidth - oSpanWidth
console.log('width', typeof width, width)
oSpan.ontouchstart = (evt) => {
console.log(evt)
var touch = evt.touches[0]; //获取第一个触点
var clientX = Number(evt.touches[0].clientX)
console.log('touches[0]...', evt.touches[0])
console.log('触摸目标在视口中的x坐标。', evt.touches[0].clientX)
var x = Number(touch.screenX) - Number(touch.pageX)
// var x = Number(touch.screenX)
var pageX = touch.pageX
console.log('oSpan距离页面左侧的距离', pageX)
console.log('oProgress距离页面左侧的距离', oProgress.getBoundingClientRect().left) // 'oProgress距离页面左侧的距离',
console.log('oSpan距离页面左侧的距离', oSpan.getBoundingClientRect().left)
let leftDistance = oSpan.getBoundingClientRect().left - oProgress.getBoundingClientRect().left
console.log('左侧距离', leftDistance)
console.log('--------')
oSpan.ontouchmove = (event) => {
// console.log('oProgress距离页面左侧的距离', oProgress.getBoundingClientRect().left)
console.log('pageX-', event.touches[0].pageX)
// console.log('screenX-', event.touches[0].pageX) // 等于pageX
var touchMove = event.touches[0];
var pageXMove = event.touches[0].pageX;
// console.log('touchMove', touchMove)
// var wX=Number(touchMove.pageX);
// var wX=Number(touchMove.screenX)-x; // 移动的距离
var wX=Number(touchMove.pageX)-pageX; // 移动的距离
console.log('移动wX', wX)
// console.log('Number(touchMove.clientX )', Number(touchMove.clientX ))
console.log('负的值', -leftDistance)
if(wX == 0) {
wX = leftDistance
} else if (wX < -leftDistance) {
wX=0;
}
else if( wX < 0 && wX >= -leftDistance )
{
}else if(wX<=width)
{
} else if (wX>width) {
wX = width
}
oSpan.style.left= leftDistance + wX+'px';
}
document.onmouseup=function(){
document.onmousemove=null;
};
}
</script>
</body>
</html>
- 快应用
<template>
<!-- template里只能有一个根节点 -->
<div class="wrapper">
<div id="box">
<!-- 进度条整体 -->
<div id="progress">
<!-- 进度条长度 -->
<div id="progress_head"></div>
<!-- 进度条移动条 -->
<!-- <stack> -->
<div
style="left: {{changeX}}px"
id="span"
ontouchstart="touchstartFn()"
ontouchmove="touchmoveFn()"
ontouchend="touchendFn()"
ontouchcancel="touchcancelFn()"
>
<text>{{ '开始-结束时间' }}</text>
</div>
<!-- </stack> -->
</div>
</div>
<!-- <text class="title">{{ title }}</text> -->
<!-- 点击跳转详情页 -->
<!-- <input
class="btn"
type="button"
value="跳转到详情页"
οnclick="onDetailBtnClick"
/> -->
</div>
</template>
<script>
import router from '@system.router'
export default {
// 页面级组件的数据模型,影响传入数据的覆盖机制:private内定义的属性不允许被覆盖
data: {
oProgressWidth: 0,
oSpanWidth: 0,
width: 0,
oProgressLeft: 0,
oSpanLeft: 0,
clientX: 0,
leftDistance: 0,
changeX: 0
},
onShow() {
setTimeout(() => {
var oSpan = this.$element('span');
// oSpan.style.transform = `translateX(${200}px)`;
this.changeX = 200
}, 3000)
//js获取节点
// var oProgress = document.getElementById('progress'); // 进度条整体的宽度
// var oProgress = this.$element('progress'); // 进度条整体的宽度
// // var oSpan = document.getElementById('span'); // 进度条滑块
// var oSpan = this.$element('span'); // 进度条滑块
// // var oProgress_head = document.getElementById('progress_head'); // 左侧覆盖区域
// var oProgress_head = this.$element('progress_head'); // 左侧覆盖区域
// var oProgressWidth = 0
// var oSpanWidth = 0
// var width = 0
// console.log('width', typeof width, width)
// var oProgressWidth = Number(oProgress.clientWidth)
// var oSpanWidth = Number(oSpan.clientWidth)
// var width = oProgressWidth - oSpanWidth
// console.log('width', typeof width, width)
},
touchendFn() {
console.log('touchendFn')
},
touchcancelFn() {
console.log('touchcancelFn')
},
touchmoveFn(event) {
var oSpan = this.$element('span');
// console.log('oProgress距离页面左侧的距离', oProgress.getBoundingClientRect().left)
console.log('clientX-', event.touches[0].clientX)
// console.log('screenX-', event.touches[0].clientX) // 等于clientX
var touchMove = event.touches[0];
var clientXMove = event.touches[0].clientX;
// console.log('touchMove', touchMove)
// var wX=Number(touchMove.clientX);
// var wX=Number(touchMove.screenX)-x; // 移动的距离
var wX = Number(touchMove.clientX) - this.clientX; // 移动的距离
console.log('移动wX', wX)
// console.log('Number(touchMove.clientX )', Number(touchMove.clientX ))
// console.log('负的值', -leftDistance)
let leftDistance = this.leftDistance
console.log('leftDistance', leftDistance)
let width = this.width
// console.log('width', this.width)
if (wX < -leftDistance) {
// wX = 0
this.changeX = 0
}
else if (wX< 0 && wX >= -leftDistance) {
this.changeX = leftDistance + wX
}
else if (wX === 0) {
// wX = 0;
this.changeX = leftDistance
}
else if (wX <= (width - leftDistance)) {
this.changeX = leftDistance + wX
// this.changeX = width
} else {
this.changeX = width
}
// oSpan.style.left = leftDistance + wX + 'px';
},
touchstartFn(evt) {
// if (this.oProgressWidth === 0 || this.oSpanWidth === 0) { return }
// var oProgress = document.getElementById('progress'); // 进度条整体的宽度
var oProgress = this.$element('progress'); // 进度条整体的宽度
// var oSpan = document.getElementById('span'); // 进度条滑块
var oSpan = this.$element('span'); // 进度条滑块
// var oProgress_head = document.getElementById('progress_head'); // 左侧覆盖区域
var oProgress_head = this.$element('progress_head'); // 左侧覆盖区域
this.getElementData(oProgress, oSpan, (data1, data2) => {
if (!data2) {
return
} else {
this.oSpanWidth = Number(data2.width)
this.oSpanLeft = Number(data2.left)
}
if (!data1) {
return
} else {
this.oProgressWidth = Number(data1.width)
this.oProgressLeft = Number(data1.left)
}
if (this.oProgressWidth === 0 || this.oSpanWidth === 0) { return }
this.width = this.oProgressWidth - this.oSpanWidth
// console.log('this.width', this.width)
console.log('touchstartFn', evt, this.width)
this.leftDistance = this.oSpanLeft - this.oProgressLeft
// console.log('leftDistance', this.leftDistance)
// console.log('执行')
// this.startMove(data1)
var touch = evt.touches[0]; //获取第一个触点
var clientX = Number(evt.touches[0].clientX)
// console.log('touches[0]...', evt.touches[0])
// var x = Number(touch.screenX) - Number(touch.clientX)
// var x = Number(touch.screenX)
this.clientX = touch.clientX
console.log('触摸目标在视口中的x坐标。', evt.touches[0].clientX)
})
// var touch = evt.touches[0]; //获取第一个触点
// var clientX = Number(evt.touches[0].clientX)
// console.log('touches[0]...', evt.touches[0])
// console.log('触摸目标在视口中的x坐标。', evt.touches[0].clientX)
// var x = Number(touch.screenX) - Number(touch.clientX)
// // var x = Number(touch.screenX)
// var clientX = touch.clientX
// console.log('oSpan距离页面左侧的距离', clientX)
// console.log('oProgress距离页面左侧的距离', oProgress.getBoundingClientRect().left) // 'oProgress距离页面左侧的距离',
// console.log('oSpan距离页面左侧的距离', oSpan.getBoundingClientRect().left)
// let leftDistance = oSpan.getBoundingClientRect().left - oProgress.getBoundingClientRect().left
// console.log('左侧距离', leftDistance)
// console.log('--------')
},
getElementData(oProgress, oSpan, callback) {
const promiseA = new Promise((resolutionFunc, rejectionFunc) => {
oProgress.getBoundingClientRect({
success: (data) => {
resolutionFunc(data);
}
})
});
const promiseB = new Promise((resolutionFunc, rejectionFunc) => {
oSpan.getBoundingClientRect({
success: (data) => {
resolutionFunc(data);
}
})
});
promiseA.then(data => {
promiseB.then(dat => {
console.log('data, dat', data, dat)
callback && callback(data, dat)
})
})
}
}
</script>
<style lang="scss">
#box {
position: relative;
width: 700px;
height: 60px;
margin: 100px auto;
}
#progress {
position: relative;
width: 600px;
height: 60px;
background-color: #999999;
border-radius: 8px;
margin: 0 auto;
}
#progress_head {
width: 0px;
height: 100%;
border-top-left-radius: 8px;
border-bottom-left-radius: 8px;
background-color: #9933cc;
}
#span {
position: absolute;
width: 80px;
height: 72px;
background-color: #9933cc;
top: -4px;
left: 0px;
/* cursor:pointer; */
}
/* @import './../../assets/styles/style.scss';
.wrapper {
@include flex-box-mixins(column, center, center);
.title {
font-size: 8 * $size-factor;
text-align: center;
color: $black;
}
.btn {
width: 90 * $size-factor;
height: 16 * $size-factor;
border-radius: 8 * $size-factor;
background-color: $brand;
color: $white;
font-size: 30px;
margin-top: 16 * $size-factor;
}
} */
</style>