前端JS——滑动滑块验证登录(源码及效果)

1.html代码

<div class="box">
			<!--滑块-->
			<a href="#"><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;//背景
		
	}
}
  • 6
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,请稍等片刻,我马上为您查找并提供该代码。以下是使用Selenium自动化测试框架编写的登录12306官网验证滑动登录成功的代码: ```python # 导入selenium相关的模块 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # 开启chrome浏览器,进入12306登录页面 driver = webdriver.Chrome() driver.get('https://kyfw.12306.cn/otn/login/init') # 显示等待直到页面完全加载 element = WebDriverWait(driver, 5).until( EC.presence_of_element_located((By.ID, 'J-loginImg')) ) # 定位到块元素 slider = driver.find_element_by_id('J-slider') # 使用ActionChains模拟鼠标操作,按住块拖动到最右边 ActionChains(driver).click_and_hold(slider).perform() ActionChains(driver).move_by_offset(300, 0).perform() # 释放鼠标 ActionChains(driver).release().perform() # 等待验证通过 WebDriverWait(driver, 5).until_not( EC.text_to_be_present_in_element((By.ID, 'J-loginImg'), '拖动完成验证') ) # 定位到登录按钮并点击 driver.find_element_by_id('J-login').click() # 验证是否登录成功 WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CLASS_NAME, 'my_order')) ) # 打印登录成功信息 print('登录成功!') # 关闭浏览器 driver.quit() ``` 以上是关于使用Selenium编写登录12306官网验证滑动登录成功的代码,希望对您有所帮助。如果您有更多的问题,请随时问我。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值