js 슬라이더 인증 로그 인 실현

본 논문 의 사례 는 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.clientX X 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;//  
 
 }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기