html代码: <div class="bar"> <div class="line"> <div class="dot"></div> </div> </div> //js: // 获取进度条 var bar = document.querySelector(".bar"); var line = document.querySelector(".line"); var dot = document.querySelector(".dot"); dot.addEventListener('mousedown', function () { document.addEventListener('mousemove', move); function move(e){ // 获取鼠标到bar左边的距离 var x = e.pageX - bar.offsetLeft; // 当鼠标位置小于进度条最小位置 if ( e.pageX < bar.offsetLeft) { line.style.width = 0 + "px"; dot.style.left = 0 + "px"; } // 大于 else if( e.pageX > bar.offsetLeft + bar.offsetWidth){ line.style.width = bar.width + "px"; dot.style.left = bar.width + "px"; }else{ line.style.width = x + "px"; dot.style.left = x + "px"; } } document.addEventListener('mouseup', function () { // 移除由addEventListener添加的方法 document.removeEventListener('mousemove',move); }) })