Home > Web Front-end > JS Tutorial > Implement a slider verification function

Implement a slider verification function

一个新手
Release: 2018-05-18 09:26:22
Original
3948 people have browsed it

I recently wrote something small, a sliding verification

<em><html>
<head>
	<meta charset="UTF-8">
	<title></title>
	<style type="text/css">
		*{
			margin: 0px;
			padding: 0px;
		}
		body{
			background: #000000;
		}
		.sliding{
			width: 400px;
			height: 40px;
			margin: 50px auto;
			background: #ccc;
			text-align: center;
			line-height: 40px;
			position: relative;
		}
		.sliding > p{
			width: 0px;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			background: green;
			z-index: 99;
			color: #FFFFFF;
			text-align: center;
			overflow: hidden;
		}
		.sliding > span{
			display: block;
			width: 46px;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			background-size: cover;
			cursor: pointer;
			z-index: 999;
			}
		.span1_back{
			background: url(img/sli1.png) no-repeat center center;
		}
		.span2_back{
			background: url(img/sli2.png)no-repeat center center;
		}
	</style>
</head>
<body>
	<p class="sliding">
		请按住滑块,拖至最右侧。
		<p></p>
		<span class="span1_back">
		</span>
	</p>
	<!-- 布局方面主要是使用定位把p标签和span都定位在 p 左边 --><br/><br/>
	<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		$(function(){
			
			$(&#39;.sliding > span&#39;).on(&#39;mousedown&#39;,function(ev){
				var down_X=ev.clientX;     //获取鼠标摁下的位置
				var p_W=$(&#39;.sliding > p&#39;).get(0).offsetWidth;   //获取p的宽度当然这其实这个可有可无
				var span_X=$(&#39;.sliding > span&#39;).get(0).offsetLeft;  //与上同理
				$(document).get(0).onmousemove=function(ev){
					var move_X=ev.clientX; //获取鼠标移动的位置
					var size = move_X - down_X + p_W; //用移动过后的位置减去鼠标摁下的位置加上p的宽度或者 span 的left 值 因为俩者是同步的 所以 减去 其中一个就好					
					if(size>0){// 移动的时候判断size>0的时候执行,Math.min选取最小值,让拖动过程中不至于超出
						size=Math.min(($(&#39;.sliding&#39;).get(0).offsetWidth - $(&#39;.sliding > span&#39;).get(0).offsetWidth),size);
					}else{ //小于0的时候 size =0; 防止有无聊的用户拖回 超出边界。 
						size=0;
					}
					$(&#39;.sliding > p&#39;).css(&#39;width&#39;, size + &#39;px&#39;); 
					$(&#39;.sliding > span&#39;).css(&#39;left&#39;, size + &#39;px&#39;);
					return false;//防止拖拽过程中选中文字
				}
				$(document).get(0).onmouseup=function(){ //鼠标松开执行<br/>                                              //判段span到达边界时候执行
					if(($(&#39;.sliding > span&#39;).get(0).offsetLeft) >= ($(&#39;.sliding&#39;).get(0).offsetWidth - $(&#39;.sliding > span&#39;).get(0).offsetWidth)){
						$(&#39;.sliding > span&#39;).attr(&#39;class&#39;,&#39;span2_back&#39;); //更改span的背景图片
						$(&#39;.sliding > p&#39;).text(&#39;验证通过&#39;); //更改p标签中的内容
						$(&#39;.sliding > span&#39;).off(&#39;mousedown&#39;);//删除鼠标摁下方法防止用户 无聊往回 推拽
					}else{//判断没到达边界让它再回到起点
						$(&#39;.sliding > span&#39;).animate({left:&#39;0px&#39;},300); //利用jquery让p 和 span 回到起始状态
						$(&#39;.sliding > p&#39;).animate({width:&#39;0px&#39;},300);
					}
					$(document).get(0).onmousemove=null; //最后让 鼠标移动 和 鼠标松开 事件停止  
					$(document).get(0).onmouseup=null;
				}
			})
				
		})
	</script>
</body>
</html></em>
Copy after login

The above is the detailed content of Implement a slider verification function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template