av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

原生JavaScript實(shí)現(xiàn)拖動(dòng)校驗(yàn)功能

瀏覽:3日期:2023-06-16 11:21:49

本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)拖動(dòng)校驗(yàn)的具體代碼,供大家參考,具體內(nèi)容如下

思路

1、頁(yè)面布局采用定位,背景顏色變化bg的寬度為0,其寬度會(huì)隨著滑塊的移動(dòng)而移動(dòng)。

頁(yè)面結(jié)構(gòu)

<!--驗(yàn)證--><div class='box'> <!--滑塊--> <div class='btn'></div> <!--文字--> <p class='text'>請(qǐng)滑動(dòng)滑塊</p> <!--背景--> <div class='bg'></div></div>

頁(yè)面布局

/* 滑塊使用定位,背景沒(méi)有設(shè)置寬度*/.box { width: 250px; height: 50px; background-color: #ccc; position: relative; margin: 0 auto;}.btn { box-sizing: border-box; width: 50px; height: 50px; border: 1px solid #ccc; color: #ccc; background-color: #fff; position: absolute; left: 0; top: 0; cursor: pointer; z-index: 4;}.text { position: absolute; height: 50px; left: 50%; transform: translateX(-50%); z-index: 2; user-select: none;}.bg { width: 0; height: 50px; background-color: #25c20f; z-index: 3; position: absolute; top: 0; left: 0;}

2、分析事件—鼠標(biāo)按下,鼠標(biāo)移動(dòng),鼠標(biāo)松開(kāi)

2.1 鼠標(biāo)按下,獲取此時(shí)事件的水平距離downX;鼠標(biāo)移動(dòng),獲取此時(shí)事件的水平距離e.clientX;那么鼠標(biāo)移動(dòng)的距離moveX = e.clientX - downX,也就是滑塊跟著移動(dòng)的距離。即btn.style.left = moveX + ’px’;同時(shí)bg的寬度也就是滑塊移動(dòng)的距離,即bg.style.width = moveX + ’px’

2.2 滑塊拉到頭了,表示驗(yàn)證成功什么時(shí)候表示滑塊滑到頭了,也就是moveX等于box的寬度-滑塊的寬度。那么文字的改變成“驗(yàn)證成功”。且滑塊停留在了最有端。無(wú)論鼠標(biāo)點(diǎn)擊還是移動(dòng),都不會(huì)在影響了。那就是清除事件,清除按鈕的鼠標(biāo)移動(dòng)和鼠標(biāo)按下事件btn.onmousemove = null; btn.onmousedown = null;//清除事件此時(shí)驗(yàn)證成功,設(shè)立一個(gè)標(biāo)記為表示驗(yàn)證成功flag=true,后續(xù)需要用到。

2.3 那么如果我們滑塊拉到一半就松開(kāi)了鼠標(biāo),滑塊應(yīng)該回到原始位置。但是如果已經(jīng)驗(yàn)證成功了,那就不會(huì)回到原點(diǎn)。鼠標(biāo)松開(kāi)事件觸發(fā),那么鼠標(biāo)移動(dòng)已經(jīng)不能影響滑塊了,那么此時(shí)需要清除移動(dòng)事件btn.onmousemove = null;沒(méi)有驗(yàn)證成功那就回到原點(diǎn)this.style.left = 0; bg.style.width = 0;

頁(yè)面動(dòng)作

function selector(name) { return document.querySelector(name);}var box = selector(’.box’), btn = selector(’.btn’), text = selector(’.text’), bg = selector(’.bg’), flag = false;// 鼠標(biāo)按下,移動(dòng),松開(kāi)// 按下的距離和移動(dòng)的距離差就是滑塊移動(dòng)的距離btn.onmousedown = function (e) {//按鈕按下的 var downX = e.clientX btn.onmousemove = function(e){//e 事件的狀態(tài) var moveX = e.clientX - downX; if(moveX > 0) { this.style.left = moveX + ’px’; bg.style.width = moveX + ’px’ // 滑塊拉到頭了,表示驗(yàn)證成功 if (moveX >= box.offsetWidth - this.offsetWidth) {bg.style.zIndex = 1;// 設(shè)置bg的z-index的值是為了處理黨滑塊經(jīng)過(guò)原始值的時(shí)候,bg將文字覆蓋了。驗(yàn)證成功后,有讓文字顯示出來(lái)text.innerText = ’驗(yàn)證成功’;text.style.color = ’#fff’;flag = true;// 此時(shí)鼠標(biāo)移動(dòng)或者按下,滑塊不在跟著移動(dòng)了btn.onmousemove = null;//btn.onmousedown = null;//清除事件 } } }}btn.onmouseup = function () { btn.onmousemove = null; // 如果驗(yàn)證成功了,那就不會(huì)回到原點(diǎn) if(flag){ return ; } this.style.left = 0; bg.style.width = 0;}

完整可以運(yùn)行的源碼

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title> <style> /* 滑塊使用定位,背景沒(méi)有設(shè)置寬度*/ .box { width: 250px; height: 50px; background-color: #ccc; position: relative; margin: 0 auto; } .btn { box-sizing: border-box; width: 50px; height: 50px; border: 1px solid #ccc; color: #ccc; background-color: #fff; position: absolute; left: 0; top: 0; cursor: pointer; z-index: 4; } .text { position: absolute; height: 50px; left: 50%; transform: translateX(-50%); z-index: 2; user-select: none; } .bg { width: 0; height: 50px; background-color: #25c20f; z-index: 3; position: absolute; top: 0; left: 0; } </style></head><body><!--驗(yàn)證--><div class='box'> <!--滑塊--> <div class='btn'></div> <!--文字--> <p class='text'>請(qǐng)滑動(dòng)滑塊</p> <!--背景--> <div class='bg'></div></div><script> function selector(name) { return document.querySelector(name); } var box = selector(’.box’), btn = selector(’.btn’), text = selector(’.text’), bg = selector(’.bg’), flag = false; // 鼠標(biāo)按下,移動(dòng),松開(kāi) // 按下的距離和移動(dòng)的距離差就是滑塊移動(dòng)的距離 btn.onmousedown = function (e) {//按鈕按下的 var downX = e.clientX btn.onmousemove = function(e){//e 事件的狀態(tài) var moveX = e.clientX - downX; if(moveX > 0) {this.style.left = moveX + ’px’;bg.style.width = moveX + ’px’// 滑塊拉到頭了,表示驗(yàn)證成功if (moveX >= box.offsetWidth - this.offsetWidth) { bg.style.zIndex = 1;// 設(shè)置bg的z-index的值是為了處理黨滑塊經(jīng)過(guò)原始值的時(shí)候,bg將文字覆蓋了。驗(yàn)證成功后,有讓文字顯示出來(lái) text.innerText = ’驗(yàn)證成功’; text.style.color = ’#fff’; flag = true; // 此時(shí)鼠標(biāo)移動(dòng)或者按下,滑塊不在跟著移動(dòng)了 btn.onmousemove = null;// btn.onmousedown = null;//清除事件} } } } btn.onmouseup = function () { btn.onmousemove = null; // 如果驗(yàn)證成功了,那就不會(huì)回到原點(diǎn) if(flag){ return ; } this.style.left = 0; bg.style.width = 0; }</script></body></html>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 丁香久久| 国产精品免费一区二区 | 一级大黄色片 | 精品国产黄a∨片高清在线 www.一级片 国产欧美日韩综合精品一区二区 | 神马久久av | 爱操影视 | 中文字幕在线播放不卡 | 2019精品手机国产品在线 | 国产精品一区三区 | 在线视频日韩 | 免费视频一区 | 久久久国产一区二区三区 | 国产精品18hdxxxⅹ在线 | 丁香综合| 欧美视频偷拍 | 中文字幕日本一区二区 | 99欧美精品| 超碰一区二区 | 国产激情偷乱视频一区二区三区 | 91.xxx.高清在线 | 毛片免费在线 | 久久久久久亚洲精品 | 亚洲人精品午夜 | 日韩看片 | 久久一二 | 久久久www成人免费精品 | 久久久久国产精品人 | 中文在线一区二区 | 久草a√| 91精品国产91久久综合桃花 | www.色综合| 在线免费观看亚洲 | 日韩美女在线看免费观看 | 91精品国产综合久久久久久漫画 | 日韩网站在线 | 成人av网页 | 91精品国产一区二区 | 日韩高清中文字幕 | 久久一区二区三区四区五区 | 91久久精品国产91久久性色tv | 成人小视频在线观看 |