console
var wrap = document.querySelector(".captch")
var box = document.querySelector(".box")
var boxBg = document.querySelector(".box-bg")
var x = ''
box.addEventListener("mousedown",function(e){
x = e.offsetX
box.style.transition = 'none'
boxBg.style.transition = 'none'
box.addEventListener("mousemove",run)
})
box.addEventListener("mouseup",function(e){
box.removeEventListener("mousemove",run)
init()
})
box.addEventListener("mouseout",function(e){
box.removeEventListener("mousemove",run)
init()
})
function run(e){
var left = ( e.offsetX + e.target.offsetLeft - x )
if(left < 0 ){
left = 0
}
if( left > 120 ){
left = 120
}
boxBg.style.width = left + 'px'
box.style.left = left + 'px'
}
function init(){
if( parseInt( box.style.left ) <120 ){
box.style.transition = 'all 0.3s linear 0s'
boxBg.style.transition = 'all 0.3s linear 0s'
setTimeout(function(){
box.style.left = '0px'
boxBg.style.width = '0px'
},1)
}
}
<div class="captch">
<div class="box"></div>
<div class="box-bg"></div>
</div>
body{
margin: 0;
padding: 0;
}
.captch{
width: 150px;
height: 30px;
border: 1px solid #999;
position: relative;
margin: 20px auto;
}
.box,.box-bg{
width: 30px;
height: 30px;
background-color: #999;
border: 1px solid #e8e8e8;
box-sizing: border-box;
position: absolute;
top: 0;
left: 0;
cursor: pointer;
}
.box-bg{
border: 0;
width: 0px;
background-color: #FF6666;
}