java實現(xiàn)滑動驗證解鎖
更新時間:2020年07月24日 10:30:17 作者:夏微涼秋微暖
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)滑動驗證解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了java實現(xiàn)滑動驗證解鎖的具體代碼,供大家參考,具體內(nèi)容如下

1.html:
<div class="drag">
<div class="bg"></div>
<div class="text" onselectstart="return false;">請拖動滑塊解鎖</div>
<div class="dragBtn">>></div>
</div>
<script>
/* 滑動驗證碼 */
var successRand = '';
//一、定義一個獲取DOM元素的方法
var
box = document.querySelector(".drag"),//容器
bg = document.querySelector(".bg"),//背景
text = document.querySelector(".text"),//文字
btn = document.querySelector(".dragBtn"),//滑塊
success = false,//是否通過驗證的標(biāo)志
distance = box.offsetWidth - btn.offsetWidth;//滑動成功的寬度(距離)
//二、給滑塊注冊鼠標(biāo)按下事件
btn.onmousedown = function(e){
//1.鼠標(biāo)按下之前必須清除掉后面設(shè)置的過渡屬性
btn.style.transition = "";
bg.style.transition ="";
//說明:clientX 事件屬性會返回當(dāng)事件被觸發(fā)時,鼠標(biāo)指針向?qū)τ跒g覽器頁面(或客戶區(qū))的水平坐標(biāo)。
//2.當(dāng)滑塊位于初始位置時,得到鼠標(biāo)按下時的水平位置
var e = e || window.event;
var downX = e.clientX;
//三、給文檔注冊鼠標(biāo)移動事件
document.onmousemove = function(e){
var e = e || window.event;
//1.獲取鼠標(biāo)移動后的水平位置
var moveX = e.clientX;
//2.得到鼠標(biāo)水平位置的偏移量(鼠標(biāo)移動時的位置 - 鼠標(biāo)按下時的位置)
var offsetX = moveX - downX;
//3.在這里判斷一下:鼠標(biāo)水平移動的距離 與 滑動成功的距離 之間的關(guān)系
if( offsetX > distance){
offsetX = distance;//如果滑過了終點,就將它停留在終點位置
}else if( offsetX < 0){
offsetX = 0;//如果滑到了起點的左側(cè),就將它重置為起點位置
}
//4.根據(jù)鼠標(biāo)移動的距離來動態(tài)設(shè)置滑塊的偏移量和背景顏色的寬度
btn.style.left = offsetX + "px";
bg.style.width = offsetX + "px";
//如果鼠標(biāo)的水平移動距離 = 滑動成功的寬度
if( offsetX == distance){
//1.設(shè)置滑動成功后的樣式
text.innerHTML = "驗證通過";
text.style.color = "#fff";
btn.innerHTML = "√";
btn.style.color = "green";
bg.style.backgroundColor = "lightgreen";
//2.設(shè)置滑動成功后的狀態(tài)
success = true;
//成功后,清除掉鼠標(biāo)按下事件和移動事件(因為移動時并不會涉及到鼠標(biāo)松開事件)
btn.onmousedown = null;
document.onmousemove = null;
//3.成功解鎖后的回調(diào)函數(shù)
setTimeout(function(){
successRand = new Date().getTime() + Math.random();
var obj = {};
obj.rand = successRand;
$.ajax({
type: "post",
url: projectName + "/loginC/setRand",
data: JSON.stringify(obj),
datatype: 'json',
contentType: "application/json",
success: function (data) {
//console.log(vm.parent.success);
//console.log(vm.isTest);
if (data.success == true) {
} else {
layer.alert(data.message);
// change_authimage();
}
},
error: function () {
layer.alert("請求失敗");
}
});
},1);
}
}
//四、給文檔注冊鼠標(biāo)松開事件
document.onmouseup = function(e){
//如果鼠標(biāo)松開時,滑到了終點,則驗證通過
if(success){
return;
}else{
//反之,則將滑塊復(fù)位(設(shè)置了1s的屬性過渡效果)
btn.style.left = 0;
bg.style.width = 0;
btn.style.transition = "left 1s ease";
bg.style.transition = "width 1s ease";
}
//只要鼠標(biāo)松開了,說明此時不需要拖動滑塊了,那么就清除鼠標(biāo)移動和松開事件。
document.onmousemove = null;
document.onmouseup = null;
}
}
// 復(fù)位驗證滑塊
function restDragBtn() {
/*btn.style.left = 0;
bg.style.width = 0;
btn.style.transition = "left 1s ease";
bg.style.transition = "width 1s ease";
text.innerHTML = "請拖動滑塊解鎖";
btn.innerHTML = ">>>";
text.style.color = "#a9a9a9";*/
location.reload();
}
</script>
2.后端:
@RequestMapping(value="/setRand",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "設(shè)置驗證碼成功")
//@ApiImplicitParam(paramType = "query",name= "username" ,value = "用戶名",dataType = "string")
/*public void userLogin(@RequestParam(value = "username" , required = false) String username,
@RequestParam(value = "password" , required = false) String password)*/
public Message setRand(@RequestBody JSONObject json,HttpServletRequest request){
Message message = new Message();
String rand = json.getString("rand");
if(StringUtils.isNotBlank(rand)){
// 將認(rèn)證碼存入redis
HttpSession httpSession = request.getSession();
redisUtil.set(httpSession.getId() + ".rand",rand);
redisUtil.expire(httpSession.getId() + ".rand",60);
message.setSuccess(true);
}else{
message.setMessage("發(fā)生異常,請刷新重試");
}
return message;
}
3.登錄驗證時:
// 驗證驗證碼
String randInput = json.getString("rand");
String rand = (String) redisUtil.get(httpSession.getId() + ".rand");
if(randInput==null||!randInput.equals(rand)) {
message.setMessage("驗證碼驗證失敗");
// 清空驗證碼
redisUtil.set(httpSession.getId() + ".rand","");
return message;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis的mapper.xml中if標(biāo)簽test判斷的用法說明
這篇文章主要介紹了Mybatis的mapper.xml中if標(biāo)簽test判斷的用法說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
Java中的StringTokenizer實現(xiàn)字符串切割詳解
這篇文章主要介紹了Java中的StringTokenizer實現(xiàn)字符串切割詳解,java.util工具包提供了字符串切割的工具類StringTokenizer,Spring等常見框架的字符串工具類(如Spring的StringUtils),需要的朋友可以參考下2024-01-01
DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題
這篇文章主要介紹了DynamicDataSource怎樣解決多數(shù)據(jù)源的事務(wù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

