今天由於工作不是很忙,就抽了一些時間來寫一個登陸滑塊驗證的代碼,由於小子初學,在網上借鑒了一些別人的代碼,搗鼓了很久終於把效果實現了。ps:小子初來咋到,技術確實有限,代碼中也許有許多不嚴謹的地方,請各位大神多多指出。
效果截圖:
不多說,先上代碼。
HTML代碼:
1 <div class="wrap"> 2 <div class="box"> 3 <div class="btn" id="dragEle"></div> 4 <div class="tips">>>拖動滑塊驗證<<</div> 5 </div> 6 <input type="button" value="提交驗證" id="submit" /> 7 </div>
CSS代碼:
1 body { 2 margin: 0; 3 padding: 0; 4 } 5 6 input{ 7 appearance:none; 8 -moz-appearance:none; 9 -webkit-appearance:none; 10 background: none; 11 border:none; 12 } 13 14 .wrap{ 15 margin: 200px 0 0 200px; 16 } 17 18 .box { 19 position: relative; 20 width: 200px; 21 height: 30px; 22 border-radius: 20px; 23 background: #686B69; 24 line-height: 30px; 25 overflow: hidden; 26 margin-bottom: 40px; 27 color: #fff; 28 font-size: 12px; 29 } 30 31 .btn { 32 position: absolute; 33 top: 0; 34 left: 0; 35 height: 30px; 36 width: 30px; 37 background: #0c7; 38 border-radius: 20px; 39 text-align: center; 40 } 41 42 .tips { 43 text-align: center; 44 } 45 46 #submit{ 47 line-height: 28px; 48 border-radius: 3px; 49 background: #0c7; 50 width: 200px; 51 text-align: center; 52 color: #fff; 53 }
JS代碼:
<script type="text/javascript"> function DragValidate (dargEle,msgEle){ var dragging = false;//滑塊拖動標識 var iX; dargEle.mousedown(function(e) { msgEle.text(""); dragging = true; iX = e.clientX; //獲取初始坐標 }); $(document).mousemove(function(e) { if (dragging) { var e = e || window.event; var oX = e.clientX - iX; if(oX < 30){ return false; }; if(oX >= 210){//容器寬度+10 oX = 200; return false; }; dargEle.width(oX + "px"); //console.log(oX); return false; }; }); $(document).mouseup(function(e) { var width = dargEle.width(); if(width < 200){ //console.log(width); dargEle.width("30px"); msgEle.text(">>拖動滑塊驗證<<"); }else{ dargEle.attr("validate","true").text("驗證成功!").unbind("mousedown"); }; dragging = false; }); }; DragValidate($("#dragEle"),$(".tips")); $("#submit").click(function(){ if(!$("#dragEle").attr("validate")){ alert("請先拖動滑塊驗證!"); }else{ alert("驗證成功!"); }; }); </script>