在工作中,有一個進度條的效果,左邊是一個input,兩者存在着密不可分的聯系,引用的插件卻出現了點小BUG,一怒之下自己寫了一個。
HTML
<div> <input type="text" id="input" value="" placeholder="0" autocomplete="off">% <div id="tiao"> <div id="yuan"></div> <div id="jindu"></div> </div> </div>
CSS
html,body{ height: 100%; } body{ margin: 0; } body>div{ padding-top: 60px; margin-left: 60px; } #tiao{ display: inline-block; width: 200px; height: 4px; background: #ddd; border-radius: 3px; position: relative; margin-left: 50px; } input{ border: 1px solid #ddd; width: 100px; text-align: center; vertical-align: top; } #yuan{ display: inline-block; width: 12px; height: 12px; border-radius: 50% 50%; background: #177ad8; position: absolute; margin-top: -4px; cursor: pointer; box-shadow: 0px 0px 3px #177ad8; } #jindu{ width: 0px; height: 4px; background-color: #177ad8; border-radius: 3px; }
JS
var scale =function(input,yuan,jindu,tiao){ this.input =document.getElementById(input); this.yuan =document.getElementById(yuan); this.jindu=document.getElementById(jindu); this.tiao=document.getElementById(tiao); this.tiaoW =this.tiao.offsetWidth; this.init() } scale.prototype ={ init: function(){ var isfalse =false, f = this, m = Math, b = document.body, minValue =0, maxValue =100; f.yuan.onmousedown =function(e){ isfalse =true; var X =e.clientX; var offleft =this.offsetLeft; var max = f.tiao.offsetWidth - this.offsetWidth; b.onmousemove =function(e){ if (isfalse == false){ return; } var changeX= e.clientX; var moveX =m.min(max,m.max(-2,offleft+(changeX-X))); f.input.value =m.round(m.max(0,moveX / max) * 100); f.yuan.style.marginLeft =m.max(0, moveX) +"px"; f.jindu.style.width =moveX +"px"; } } b.onmouseup =function(){ isfalse =false; } f.input.onblur =function(){ var theV =this.value*1; if(theV >maxValue || theV <minValue){ alert("輸入的數值不正確"); f.input.value =""; f.yuan.style.marginLeft ="0px"; f.jindu.style.width ="0px"; return; } var xxx =theV/100*f.tiaoW; f.yuan.style.marginLeft =xxx +"px"; f.jindu.style.width =xxx +"px"; } } } new scale("input","yuan","jindu","tiao");
封裝了一下,感覺更麻煩了。。。每次封裝都有這樣的感覺,畢竟小菜鳥一枚,若是有更優化的方法望看到的園友指正。。。
關於數學,關於邏輯,都是我的弱勢,思考起來常常腦海一片混亂,所以自己在編寫的時候也會加上注釋,放一波代碼
var input =document.getElementById("input"); var yuan =document.getElementById("yuan"); var jindu =document.getElementById("jindu"); var tiao =document.getElementById("tiao"); var tiaoW =tiao.offsetWidth; function schedule(){ var isfalse =false; //控制滑動 yuan.onmousedown =function(e){ isfalse =true; var X =e.clientX; //獲取當前元素相對於窗口的X左邊 var offleft =this.offsetLeft; //當前元素相對於父元素的左邊距 var max = tiao.offsetWidth - this.offsetWidth; //寬度的差值 document.body.onmousemove=function(e){ if (isfalse == false){ return; } var changeX= e.clientX; //在移動的時候獲取X坐標 var moveX =Math.min(max,Math.max(-2,offleft+(changeX-X))); //超過總寬度取最大寬 input.value =Math.round(Math.max(0,moveX / max) * 100); yuan.style.marginLeft =Math.max(0, moveX) +"px"; jindu.style.width =moveX +"px"; } } document.body.onmouseup =function(){ isfalse =false; } var minValue =0; var maxValue =100; input.onblur =function(){ var theV =this.value*1; if(theV >maxValue || theV <minValue){ alert("輸入的數值不正確"); input.value =""; jindu.style.width ="0px"; yuan.style.marginLeft ="0px"; return; } var xxx =theV/100*tiaoW; yuan.style.marginLeft =xxx +"px"; jindu.style.width =xxx +"px"; } } schedule();
這個看起來易懂點。。。 點擊這里看效果