<!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>
* {
margin: 0;
padding: 0;
}
.progress_bar {
width: 700px;
height: 35px;
margin: 50px auto;
position: relative;
}
.progress_bar .left {
width: 650px;
height: 100%;
background-color: #e5e5e5;
float: left;
}
.progress_bar .right {
width: 50px;
height: 100%;
text-align: center;
line-height: 35px;
float: left;
}
.progress_bar .left .bg {
width: 0px;
height: 35px;
background-color: red;
}
.progress_bar .left .bar {
width: 45px;
height: 51px;
background-color: red;
position: absolute;
top: -8px;
left: 0;
cursor: move;
}
</style>
</head>
<body>
<div class="progress_bar">
<div class="left">
<div class="bg"></div>
<div class="bar"></div>
</div>
<div class="right">0%</div>
</div>
<script>
let bar = document.querySelector('.left>.bar')
let bg = document.querySelector('.left>.bg')
let right = document.querySelector('.right')
bar.οnmοusedοwn=function(e){
e=e||window.event //获取对象
let l=e.offsetX //得到光标相对于自身的X轴
document.οnmοusemοve=function(e){
e=e||window.event
let x=e.clientX-bar.parentElement.parentElement.offsetLeft-l
if(x<=0){
x=0
}
else if(x>=bar.parentElement.offsetWidth-bar.offsetWidth){
x=bar.parentElement.offsetWidth-bar.offsetWidth
}
bar.style.left=x+'px'
bg.style.width=x+'px'
right.innerHTML=parseInt(x/(bar.parentElement.offsetWidth-bar.offsetWidth)*100)+'%'
}
document.οnmοuseup=function(){
document.οnmοusemοve=null
}
return false
}
console.log(doucument);
</script>
</body>
</html>