本文实例为大家分享了js实现滑动滑块验证登录的具体代码,供大家参考,具体内容如下
1.html代码
<div class="box"> <!--滑块--> <a href="#" rel="external nofollow" ><div class="btn"></div></a> <!--文字--> <p class="text">拖动滑块验证</p> <!--背景--> <div class="bg"></div> </div>
2.css样式
最大的盒子相对定位,其他内部内容绝对定位
需要根据层级设置z-index保证滑动的正常使用
.box{ position: relative; width: 300px; height: 34px; background: #e8e8e8; border-radius: 4px; left: 20px; } .btn{ position: absolute; top: 0; width: 40px; height:32px; text-align: center; line-height: 32px; border-radius: 4px; z-index: 3; background-color: #fff; border: 1px solid #ccc; color: black; } .text{ position: absolute; width: 100%; margin: 0; text-align: center; line-height: 34px; display: block; z-index: 2; /*-webkit-margin-before: 1em; -webkit-margin-after: 1em;*/ } .bg{ position: absolute; height: 100%; background-color: yellowgreen; z-index: 1; }
样式
3.js事件
分析使用过程:按住滑块并拖动可以移动,中途松开滑块返回起始位置,拖动至最后滑块不动
分析动作:
1.按钮按下并移动
2.事件状态:event对象(鼠标位置)event.clientX获得与X轴的距离
3.松开按钮回到原处
4.结束,松开按钮,按钮不可再次拖动
1)
var btn=document.querySelector(".btn"); var box=document.querySelector(".box"); var bg=document.querySelector(".bg"); var text=document.querySelector(".text");
或者使用封装选择器
function $(name){ return document.querySelector(name); }; var box=$(".box"),btn=$(".btn").....;
2)按下
按下后获得与x轴的距离
btn.onmousedown=function(e){ var downX=e.clientX;
3)拖动
拖动后获得与x轴距离减去初始值距离得到按钮移动的值
根据移动的值:判断按钮是否可以正常移动,判断按钮是否已经完成验证
btn.onmousemove=function(e){ var moveX=e.clientX-downX; // console.log(moveX); //移动范围 if(moveX>-2){ this.style.left=moveX+"px";//将移动值赋值给滑块 bg.style.width=moveX+"px";//背景 if(moveX>=(box.offsetWidth-btn.offsetWidth)){//包含原始宽度内边距边框,不包含外边框 //拖到头,验证成功 flag=true; text.innerHTML="验证成功"; text.style.color="white"; //事件清除 btn.onmousedown=null; btn.onmousemove=null; } }
4)松开按钮
回到原处清除拖动
btn.onmouseup=function(){ //事件清除 btn.onmousemove=null; if(flag)return; this.style.left=0;//将移动值赋值给滑块 bg.style.width=0;//背景 }
4.效果
5.源码
//原生写法 window.onload=function(){ var btn=document.querySelector(".btn"); var box=document.querySelector(".box"); var bg=document.querySelector(".bg"); var text=document.querySelector(".text"); //封装选择器 // function $(name){ // return document.querySelector(name); // }; // var box=$(".box"),btn=$(".btn").....; var flag=false; //按下onmousedown 拖动onmousemove //document.querySelector(".btn").onmousedown=function(event){//event事件状态 // var e=event||window.event; //获取方法集合,可直接通过id, 类, 类型, 属性, 属性值等来选取元素(返回此名字的第一个)。 btn.onmousedown=function(e){//按下 var downX=e.clientX; //按下后对x轴的距离 // console.log(downX); // alert("1"); btn.onmousemove=function(e){//拖动 var moveX=e.clientX-downX; //拖动后与x轴距离减去初始值距离,移动值 // console.log(moveX); //移动范围 if(moveX>-2){ this.style.left=moveX+"px";//将移动值赋值给滑块 bg.style.width=moveX+"px";//背景 if(moveX>=(box.offsetWidth-btn.offsetWidth)){//包含原始宽度内边距边框,不包含外边框 //拖到头,验证成功 flag=true; text.innerHTML="验证成功"; text.style.color="white"; //事件清除 btn.onmousedown=null; btn.onmousemove=null; } } } } //松开按钮 btn.onmouseup=function(){ //事件清除 btn.onmousemove=null; if(flag)return; this.style.left=0;//将移动值赋值给滑块 bg.style.width=0;//背景 } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 胡杨林.2011-爱上了瘾(EP)【喜欢音乐】【WAV+CUE】
- 仙境传说新启航2024公测可用礼包码大全 仙境传说兑换码礼包大全
- 魔兽世界地心之战神牧用什么食物合剂 地心之战神牧食物合剂推荐
- 魔兽世界地心之战神牧用什么附魔宝石 地心之战神牧附魔宝石推荐
- 明达年度发烧碟MasterSuperiorAudiophile2019[DSF]
- 明达年度发烧碟MasterSuperiorAudiophile2020[DSF]
- 【发烧唱片】Naim《示范碟(第二辑)》1999[WAV+CUE]
- 《优米雅的炼金工房》“妮娜”战斗实机:大雷黑丝金发御姐
- 许冠杰.2003-四合一珍藏集4CD【宝丽金】【WAV+CUE】
- 李宇春.2014-1987我不知会遇见你【天娱传媒】【WAV+CUE】
- 梁汉文.1995-抱着你感觉很好【华星】【WAV+CUE】
- 《情歌回首 HIFI国语老歌 2CD》[WAV/分轨][1.2GB]
- 《降央卓玛 草原绝色醇美的歌声 金色的卓玛》[WAV/分轨][520MB]
- 《赵雷 30首经典城市民谣 2CD》[WAV/分轨][1.3GB]
- 仙境传说新启航兑换码怎么使用 仙境传说兑换码使用方法