vue 登錄滑動驗證實現(xiàn)代碼
更新時間:2018年08月24日 10:03:08 作者:前端小白16
這篇文章主要介紹了vue 登錄滑動驗證實現(xiàn)代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
在沒給大家講解實現(xiàn)代碼之前,先給大家分享效果圖:
之前別人都是用jq寫的,自己整理了一下開始使用
<el-form-item label="驗證"> <div class="form-inline-input"> <div class="code-box" id="code-box"> <input type="text" name="code" class="code-input" /> <p></p> <span>>>></span> </div> </div> </el-form-item>
vue代碼
//獲取元素距離頁面邊緣的距離 getOffset(box,direction){ var setDirection = (direction == 'top') ? 'offsetTop' : 'offsetLeft' ; var offset = box[setDirection]; var parentBox = box.offsetParent; while(parentBox){ offset+=parentBox[setDirection]; parentBox = parentBox.offsetParent; } parentBox = null; return parseInt(offset); }, moveCode(code,_this){ var fn = {codeVluae : code}; var box = document.querySelector("#code-box"), progress = box.querySelector("p"), codeInput = box.querySelector('.code-input'), evenBox = box.querySelector("span"); //默認事件 var boxEven = ['mousedown','mousemove','mouseup']; //改變手機端與pc事件類型 if(typeof document.ontouchstart == 'object'){ boxEven = ['touchstart','touchmove','touchend']; } var goX,offsetLeft,deviation,evenWidth,endX; function moveFn(e){ e.preventDefault(); e = (boxEven['0'] == 'touchstart') ? e.touches[0] : e || window.event; endX = e.clientX - goX; endX = (endX > 0) ? (endX > evenWidth) ? evenWidth : endX : 0; if(endX > evenWidth * 0.7){ progress.innerText = '松開驗證'; progress.style.backgroundColor = "#66CC66"; }else{ progress.innerText = ''; progress.style.backgroundColor = "#FFFF99"; } progress.style.width = endX+deviation+'px'; evenBox.style.left = endX+'px'; } function removeFn() { document.removeEventListener(boxEven['2'],removeFn,false); document.removeEventListener(boxEven['1'],moveFn,false); if(endX > evenWidth * 0.7){ progress.innerText = '驗證成功'; progress.style.width = evenWidth+deviation+'px'; evenBox.style.left = evenWidth+'px' codeInput.value = fn.codeVluae; evenBox.onmousedown = null; _this.ruleForm.verification = true; }else{ progress.style.width = '0px'; evenBox.style.left = '0px'; } }; function getOffset(box,direction){ var setDirection = (direction == 'top') ? 'offsetTop' : 'offsetLeft' ; var offset = box[setDirection]; var parentBox = box.offsetParent; while(parentBox){ offset+=parentBox[setDirection]; parentBox = parentBox.offsetParent; } parentBox = null; return parseInt(offset); }; evenBox.addEventListener(boxEven['0'], function(e) { e = (boxEven['0'] == 'touchstart') ? e.touches[0] : e || window.event; goX = e.clientX, offsetLeft = getOffset(box,'left'), deviation = this.clientWidth, evenWidth = box.clientWidth - deviation, endX; document.addEventListener(boxEven['1'],moveFn,false); document.addEventListener(boxEven['2'],removeFn,false); },false); fn.setCode = function(code){ if(code) fn.codeVluae = code; } fn.getCode = function(){ return fn.codeVluae; } fn.resetCode = function(){ evenBox.removeAttribute('style'); progress.removeAttribute('style'); codeInput.value = ''; }; return fn; }
調(diào)用
mounted(){ var _this = this; // window.addEventListener('load',function(){ //code是后臺傳入的驗證字符串 var code = "jsaidaisd656", codeFn = new _this.moveCode(code,_this); // }); }
驗證樣式
.form-inline-input{ border:1px solid #dadada; border-radius:5px; } .form-inline-input input, .code-box{ padding: 0 3px; width: 298px; height: 40px; color: #fff; text-shadow: 1px 1px 1px black; background: #efefef; border: 0; border-radius: 5px; outline: none; } .code-box{ position: relative; } .code-box p, .code-box span{ display:block; position: absolute; left: 0; height: 40px; text-align: center; line-height: 40px; border-radius: 5px; padding:0; margin:0; } .code-box span{ width: 40px; background-color:#fff; font-size: 16px; cursor: pointer; margin-right:1px; } .code-box p{ width: 0; background-color: #FFFF99; overflow: hidden; text-indent: -20px; transition: background 1s ease-in; } .code-box .code-input{ display: none; } .code-box .code-input{ display: none; } .form-inline-input{ border:1px solid #dadada; border-radius:5px; } .form-inline-input input, .code-box{ padding: 0 3px; width: 298px; height: 40px; color: #fff; text-shadow: 1px 1px 1px black; background: #efefef; border: 0; border-radius: 5px; outline: none; } .code-box{ position: relative; } .code-box p, .code-box span{ display:block; position: absolute; left: 0; height: 40px; text-align: center; line-height: 40px; border-radius: 5px; padding:0; margin:0; } .code-box span{ width: 40px; background-color:#fff; font-size: 16px; cursor: pointer; margin-right:1px; } .code-box p{ width: 0; background-color: #FFFF99; overflow: hidden; text-indent: -20px; transition: background 1s ease-in; } .code-box .code-input{ display: none; } .code-box .code-input{ display: none; }
總結(jié)
以上所述是小編給大家介紹的vue 登錄滑動驗證實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Vue新手指南之創(chuàng)建第一個vue-cli腳手架程序
vue-cli 是一個官方發(fā)布 vue.js 項目腳手架,使用 vue-cli 可以快速創(chuàng)建 vue 項目。這篇文章主要給大家介紹了關(guān)于Vue新手指南之創(chuàng)建第一個vue-cli程序的相關(guān)資料,需要的朋友可以參考下2021-05-05使用 Vue 3 的 createApp方法初始化應用的基本步驟
createApp 是 Vue 3 引入的全局 API,用于創(chuàng)建一個應用實例,這篇文章主要介紹了如何使用 Vue 3 的 createApp方法初始化應用,通過 createApp 方法,我們從 Vue 3 的基本初始化開始,擴展到插件的應用、多個應用實例的創(chuàng)建等,需要的朋友可以參考下2024-05-05vue數(shù)據(jù)更新了但在頁面上沒有顯示出來的解決方法
有時候 vue 無法監(jiān)聽到數(shù)據(jù)的變化,導致數(shù)據(jù)變化但是視圖沒有變化,也就是數(shù)據(jù)更新了,但在頁面上沒有顯示出來,所以本文給出了三種解決方法,通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-12-12