js實(shí)現(xiàn)滑動(dòng)滑塊驗(yàn)證登錄
本文實(shí)例為大家分享了js實(shí)現(xiàn)滑動(dòng)滑塊驗(yàn)證登錄的具體代碼,供大家參考,具體內(nèi)容如下
1.html代碼
<div class="box"> <!--滑塊--> <a href="#" rel="external nofollow" ><div class="btn">>></div></a> <!--文字--> <p class="text">拖動(dòng)滑塊驗(yàn)證</p> <!--背景--> <div class="bg"></div> </div>
2.css樣式
最大的盒子相對(duì)定位,其他內(nèi)部?jī)?nèi)容絕對(duì)定位
需要根據(jù)層級(jí)設(shè)置z-index保證滑動(dòng)的正常使用
.box{
position: relative;
width: 300px;
height: 34px;
background: #e8e8e8;
border-radius: 4px;
left: 20px;
}
.btn{
position: absolute;
top: 0;
width: 40px;
height:32px;
text-align: center;
line-height: 32px;
border-radius: 4px;
z-index: 3;
background-color: #fff;
border: 1px solid #ccc;
color: black;
}
.text{
position: absolute;
width: 100%;
margin: 0;
text-align: center;
line-height: 34px;
display: block;
z-index: 2;
/*-webkit-margin-before: 1em;
-webkit-margin-after: 1em;*/
}
.bg{
position: absolute;
height: 100%;
background-color: yellowgreen;
z-index: 1;
}
樣式

3.js事件
分析使用過程:按住滑塊并拖動(dòng)可以移動(dòng),中途松開滑塊返回起始位置,拖動(dòng)至最后滑塊不動(dòng)
分析動(dòng)作:
1.按鈕按下并移動(dòng)
2.事件狀態(tài):event對(duì)象(鼠標(biāo)位置)event.clientX獲得與X軸的距離
3.松開按鈕回到原處
4.結(jié)束,松開按鈕,按鈕不可再次拖動(dòng)
1)
var btn=document.querySelector(".btn");
var box=document.querySelector(".box");
var bg=document.querySelector(".bg");
var text=document.querySelector(".text");
或者使用封裝選擇器
function $(name){
return document.querySelector(name);
};
var box=$(".box"),btn=$(".btn").....;
2)按下
按下后獲得與x軸的距離
btn.onmousedown=function(e){
var downX=e.clientX;
3)拖動(dòng)
拖動(dòng)后獲得與x軸距離減去初始值距離得到按鈕移動(dòng)的值
根據(jù)移動(dòng)的值:判斷按鈕是否可以正常移動(dòng),判斷按鈕是否已經(jīng)完成驗(yàn)證
btn.onmousemove=function(e){
var moveX=e.clientX-downX;
// console.log(moveX);
//移動(dòng)范圍
if(moveX>-2){
this.style.left=moveX+"px";//將移動(dòng)值賦值給滑塊
bg.style.width=moveX+"px";//背景
if(moveX>=(box.offsetWidth-btn.offsetWidth)){//包含原始寬度內(nèi)邊距邊框,不包含外邊框
//拖到頭,驗(yàn)證成功
flag=true;
text.innerHTML="驗(yàn)證成功";
text.style.color="white";
//事件清除
btn.onmousedown=null;
btn.onmousemove=null;
}
}
4)松開按鈕
回到原處清除拖動(dòng)
btn.onmouseup=function(){
//事件清除
btn.onmousemove=null;
if(flag)return;
this.style.left=0;//將移動(dòng)值賦值給滑塊
bg.style.width=0;//背景
}
4.效果

5.源碼
//原生寫法
window.onload=function(){
var btn=document.querySelector(".btn");
var box=document.querySelector(".box");
var bg=document.querySelector(".bg");
var text=document.querySelector(".text");
//封裝選擇器
// function $(name){
// return document.querySelector(name);
// };
// var box=$(".box"),btn=$(".btn").....;
var flag=false;
//按下onmousedown 拖動(dòng)onmousemove
//document.querySelector(".btn").onmousedown=function(event){//event事件狀態(tài)
// var e=event||window.event;
//獲取方法集合,可直接通過id, 類, 類型, 屬性, 屬性值等來選取元素(返回此名字的第一個(gè))。
btn.onmousedown=function(e){//按下
var downX=e.clientX; //按下后對(duì)x軸的距離
// console.log(downX);
// alert("1");
btn.onmousemove=function(e){//拖動(dòng)
var moveX=e.clientX-downX; //拖動(dòng)后與x軸距離減去初始值距離,移動(dòng)值
// console.log(moveX);
//移動(dòng)范圍
if(moveX>-2){
this.style.left=moveX+"px";//將移動(dòng)值賦值給滑塊
bg.style.width=moveX+"px";//背景
if(moveX>=(box.offsetWidth-btn.offsetWidth)){//包含原始寬度內(nèi)邊距邊框,不包含外邊框
//拖到頭,驗(yàn)證成功
flag=true;
text.innerHTML="驗(yàn)證成功";
text.style.color="white";
//事件清除
btn.onmousedown=null;
btn.onmousemove=null;
}
}
}
}
//松開按鈕
btn.onmouseup=function(){
//事件清除
btn.onmousemove=null;
if(flag)return;
this.style.left=0;//將移動(dòng)值賦值給滑塊
bg.style.width=0;//背景
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
對(duì)google個(gè)性主頁(yè)的拖拽效果的js的完整注釋[轉(zhuǎn)]
對(duì)google個(gè)性主頁(yè)的拖拽效果的js的完整注釋[轉(zhuǎn)]...2007-04-04
js實(shí)現(xiàn)遍歷含有input的table實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)遍歷含有input的table方法,結(jié)合實(shí)例形式分析了jsp讀取數(shù)據(jù)庫(kù)動(dòng)態(tài)生成table及JavaScript遍歷table的相關(guān)技巧,需要的朋友可以參考下2015-12-12
BootStrap智能表單實(shí)戰(zhàn)系列(七)驗(yàn)證的支持
這篇文章主要介紹了BootStrap智能表單實(shí)戰(zhàn)系列(七)驗(yàn)證的支持 ,凡是涉及到用戶編輯信息然后保存的頁(yè)面,都涉及到一個(gè)數(shù)據(jù)是否符合要求的檢查,需要客服端和服務(wù)器端的校驗(yàn)的問題,本文介紹非常詳細(xì),具有參考價(jià)值,需要的朋友可以參考下2016-06-06
JS/jQ實(shí)現(xiàn)免費(fèi)獲取手機(jī)驗(yàn)證碼倒計(jì)時(shí)效果
這篇文章主要介紹了JS/jQ實(shí)現(xiàn)免費(fèi)獲取手機(jī)驗(yàn)證碼倒計(jì)時(shí)效果的相關(guān)資料,通過定義兩個(gè)接口,發(fā)送驗(yàn)證請(qǐng)求和返回?cái)?shù)據(jù)驗(yàn)證手機(jī)號(hào)和驗(yàn)證是否一致,后臺(tái)根據(jù)接口去實(shí)現(xiàn),需要的朋友可以參考下2016-06-06
js實(shí)現(xiàn)下一頁(yè)頁(yè)碼效果
本文主要介紹了js實(shí)現(xiàn)下一頁(yè)頁(yè)碼效果的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03

