Skip to content

Instantly share code, notes, and snippets.

@edwardlorilla
Created May 17, 2021 15:50
Show Gist options
  • Save edwardlorilla/a16d76bf92d74babb689e819d4b431be to your computer and use it in GitHub Desktop.
Save edwardlorilla/a16d76bf92d74babb689e819d4b431be to your computer and use it in GitHub Desktop.
JavaScript implements draggable progress bar
<body>
<ul style='width:100%;margin-top:15rem;'>
<li>
<div class = 'box'>
<div class="scale" id="bar">
<div></div>
<span id="btn"></span>
</div>
</div>
<div class='boxDesc'>
<span id="title">0</span>
</div>
</li>
</ul>
scale=function (btn,bar,title){
this.btn=document.getElementById(btn);
this.bar=document.getElementById(bar);
this.title=document.getElementById(title);
this.step=this.bar.getElementsByTagName("DIV")[0];
this.init();
};
scale.prototype={
init:function (){
var f=this,g=document,b=window,m=Math;
f.btn.onmousedown=function (e){
var x=(e||b.event).clientX;
var l=this.offsetLeft;
var max=f.bar.offsetWidth-this.offsetWidth;
g.onmousemove=function (e){
var thisX=(e||b.event).clientX;
var to=m.min(max,m.max(-2,l+(thisX-x)));
f.btn.style.left=to+'px';
f.ondrag(m.round(m.max(0,to/max)*100),to);
b.getSelection ? b.getSelection().removeAllRanges() : g.selection.empty();
};
g.onmouseup=new Function('this.nmusemve=null');
};
},
ondrag:function (pos,x){
this.step.style.width=Math.max(0,x)+'px';
this.title.innerHTML=pos+'%';
}
}
new scale('btn','bar','title');
body{
margin:50px; ;
}
.box {
width:49%;
hegiht:3rem;
line-height:3rem;
float:left;
}
.boxDesc {
width:50%;
hegiht:3rem;
line-height:0.3rem;
float:left;
}
.scale span{
background:url(scroll.gif) no-repeat;
width:8px;
height:16px;
position:absolute;
left:-2px;
top:-5px;
cursor:pointer;
}
.scale{ background-repeat: repeat-x; background-position: 0 100%; background-color: #E4E4E4; border-left: 1px #83BBD9 solid; width: 100%; height: 10px; position: relative; font-size: 0px; border-radius: 3px; }
.scale div{ background-repeat: repeat-x; background-color: green; position: absolute; height: 10px; left: 0; bottom: 0; }
li{
font-size:12px;
line-height:50px;
position:relative;
height:50px;
list-style:none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment