<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#progress {
height: 20px;
width: 500px;
background-color: #ccc;
border-radius: 8px;
position: relative;
}
#progress_pill {
position: absolute;
border-radius: 8px 0 0px 8px;
height: 100%;
background-color: red;
left: 0;
top: 0;
}
#bar {
position: absolute;
top: -5px;
left: 0;
height: 30px;
width: 20px;
background-color: red;
cursor: pointer;
}
</style>
</head>
<body>
<div style="display: flex;padding: 200px;">
<div id="progress">
<div id="progress_pill">
</div>
<span id="bar">
</span>
</div>
<span id="progress_text" style="margin-left: 20px;">0%</span>
</div>
<script>
window.onload = function () {
var progress = document.getElementById('progress')
var progress_pill = document.getElementById('progress_pill')
var progress_text = document.getElementById('progress_text')
var bar = progress.children[1]
var moveWidth = progress.offsetWidth - bar.offsetWidth
bar.onmousedown = function (event) {
var e = window.event || event
var clientX = event.clientX - bar.offsetLeft
console.log(progress.offsetWidth)
document.body.onmousemove = function (event) {
event = event || window.event;
var moveX = event.clientX - clientX
if (moveX <= 0) {
moveX = 0
} else if (moveX >= moveWidth) {
moveX = moveWidth
}
bar.style.left = moveX + 'px'
progress_pill.style.width = moveX + 'px'
progress_text.innerHTML = parseInt(moveX / moveWidth * 100) + '%'
return false
}
document.body.onmouseup = function () {
document.body.onmousemove = null
}
}
}
</script>
</body>
</html>
原生JS手写一个可拖拽的进度条
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...