vue 登录滑动验证实现代码
作者:前端小白16 时间:2024-04-28 10:55:04
在没给大家讲解实现代码之前,先给大家分享效果图:
之前别人都是用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;
}
调用
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;
}
总结
以上所述是小编给大家介绍的vue 登录滑动验证实现代码网站的支持!
来源:https://blog.csdn.net/baidu_38305286/article/details/80054288
标签:vue,登录滑动验证
0
投稿
猜你喜欢
在ASP中使用SQL语句之4:联合语句
2007-08-11 12:34:00
SQL数据库连接超时时间已到的问题
2024-01-20 15:05:53
python 数据提取及拆分的实现代码
2023-11-13 09:13:12
python日志记录模块实例及改进
2021-03-14 12:49:09
探讨Oracle中的&号问题
2024-01-22 09:13:31
Python中下划线的使用方法
2021-10-05 23:27:12
SQL Server数据库中伪列及伪列的含义详解
2024-01-27 19:19:19
MySQL修改默认引擎和字符集详情
2024-01-14 21:37:27
使用pyqt5 tablewidget 单元格设置正则表达式
2022-12-01 22:26:54
mcrypt启用 加密以及解密过程详细解析
2023-07-15 19:41:55
python密码学RSA密码解密教程
2023-07-29 17:01:47
通过数据库引擎来加速的MySQL数据库
2012-01-29 18:07:09
对python中arange()和linspace()的区别说明
2023-08-23 05:32:53
详解Visual Studio使用Git忽略不想上传到远程仓库的文件
2023-10-13 06:42:21
编写PHP脚本使WordPress的主题支持Widget侧边栏
2023-11-23 20:27:13
利用PHP实现递归删除链表元素的方法示例
2024-04-23 09:09:41
谈切图优化加速图片显示—淘宝实例
2008-01-30 12:24:00
Python json模块与jsonpath模块区别详解
2023-09-17 18:07:26
ndarray的转置(numpy.transpose()与A.T命令对比分析)
2023-11-13 05:57:27
MYSQL 无法识别中文的永久解决方法
2024-01-13 05:24:48