版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第Java實現(xiàn)滑動驗證碼(前端部分)目錄實現(xiàn)思路實現(xiàn)代碼實現(xiàn)結(jié)果
實現(xiàn)思路
1、請求后端獲得背景圖、滑塊、x、y、誤差范圍bound
2、將滑塊設(shè)置到對應(yīng)的位置:top=y
3、添加鼠標(biāo)滑動事件
實現(xiàn)代碼
后端請求(Controller)
SliderVerifyController
/**
*@author:Yang
*@create:2025-10-25
*@Description:滑動驗證
@RestController
@RequestMapping("/sliderVerify")
publicclassSliderVerifyController{
@Resource
privateSliderVerifyServicesliderVerifyService;
@GetMapping
publicResponseResultgenerateSliderVerify(){
returnsliderVerifyService.generateSliderVerify();
SliderVerifyServiceImpl
/**
*@author:Yang
*@create:2025-10-25
*@Description:
@Service
publicclassSliderVerifyServiceImplimplementsSliderVerifyService{
@Value("${my.staticPath}")
privateStringpath;
@Override
publicResponseResultgenerateSliderVerify(){
intindex=newRandom().nextInt(5)+1;
ImageSlideVerifyslideVerify=ImageSlideVerifyUtil.cutting(
newFile(System.getProperty("user.dir")+"/web_component/src/main/resources/static/sliderVerify/"+index+".png"));
returnnewResponseResult(200,slideVerify);
HTML代碼
!DOCTYPEhtml
htmllang="zh"
head
metacharset="UTF-8"
metahttp-equiv="X-UA-Compatible"content="IE=edge"
metaname="viewport"content="width=device-width,initial-scale=1.0"
title滑動驗證碼/title
/head
body
divid="box"
divid="content"
imgid="bg"src="bg.png"/
imgid="block"src="b.png"/
/div
!--滑塊--
divid="slider"
divid="slider-block"
/div
/div
/div
/body
script
letblockImg=document.querySelector("#block");//滑塊
letbgImg=document.querySelector("#bg");//背景
letsliderBlock=document.querySelector("#slider-block");//滑動欄的滑塊
functionsliderVerify(callback){
//獲取驗證碼信息
constxhr=newXMLHttpRequest();
xhr.open("get","http://localhost:8080/sliderVerify",true);
xhr.send();
xhr.onreadystatechange=function(){
if(this.readyState==4this.status==200){
letdata=JSON.parse(this.response).data;
blockImg.src=data.blockImg;
bgImg.src=data.bgImg;
blockImg.style.top=data.y+80+"px";
callbackcallback(data);//回調(diào)函數(shù)
//執(zhí)行
sliderVerify(function(data){
//添加鼠標(biāo)事件
sliderBlock.addEventListener("mousedown",(e1)={
constx=e1.x;
window.onmousemove=function(e){
letleft=e.x-x;
//設(shè)置邊界,因為我的圖片大小都是固定425,所以我直接固定
if(e1.target.offsetLeft=425){
sliderBlock.style.left=425+"px";
blockImg.style.left=425-20+"px";
return;
//移動
sliderBlock.style.left=left+"px";
blockImg.style.left=left-20+"px";
//鼠標(biāo)釋放
window.onmouseup=function(e){
//獲取滑動到的位置
letleft=sliderBlock.style.left.split("px")[0];
//減去80內(nèi)邊距并轉(zhuǎn)成數(shù)字
left=Number(left)-80;
//清空鼠標(biāo)滑動事件
window.onmousemove=null;
//回到初始位置
sliderBlock.style.left=0+"px";
blockImg.style.left=0-20+"px";
//判斷是否在范圍內(nèi)
if(left=data.x-data.boundleft=data.x+data.bound){
alert("驗證成功");
}else{
alert("驗證錯誤")
window.location.reload()
/script
style
#content{
user-select:none;
position:relative;
padding:80px;
padding-bottom:0;
#block{
position:absolute;
/*top:169px;*/
left:-20px;
#slider{
position:relative;
display:flex;
align-items:center;
box-sizing:border-box;
width:505px;
height:50px;
padding:02px;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 4928-2026啤酒分析方法
- 酒精蒸餾工崗前績效目標(biāo)考核試卷含答案
- 電動自行車裝配工保密意識考核試卷含答案
- 井下出礦工安全生產(chǎn)知識評優(yōu)考核試卷含答案
- 電子絕緣材料上膠工保密意識能力考核試卷含答案
- 橋面鋪裝質(zhì)量培訓(xùn)課件
- 銀行合規(guī)披露制度
- 酒店客房銷售與收益最大化制度
- 酒店餐飲成本控制制度
- 年產(chǎn)200萬平方米柔性電子元器件項目可行性研究報告模板-備案審批
- 杭州中考初中英語詞匯表
- 2024年國企行測題庫
- 煙囪技術(shù)在血管腔內(nèi)修復(fù)術(shù)中的應(yīng)用
- 崗位聘用登記表
- 2023年全國統(tǒng)一高考政治試卷(新課標(biāo)ⅰ)(含解析版)
- 2023年北京高考語文答題卡(北京卷)word版可編輯kh
- 2023年高鐵信號車間副主任述職報告
- GB/T 5762-2012建材用石灰石、生石灰和熟石灰化學(xué)分析方法
- 第3章 圓錐曲線的方程【精簡思維導(dǎo)圖梳理】高考數(shù)學(xué)高效備考 人教A版2019選擇性必修第一冊
- 劉一秒演說智慧經(jīng)典(內(nèi)部筆記)
- 管道TOFD檢測記錄及續(xù)表
評論
0/150
提交評論