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;//
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[2022.04.19] 자바스크립트 this - 생성자 함수와 이벤트리스너에서의 this18일에 this에 대해 공부하면서 적었던 일반적인 함수나 객체에서의 this가 아닌 오늘은 이벤트리스너와 생성자 함수 안에서의 this를 살펴보기로 했다. new 키워드를 붙여 함수를 생성자로 사용할 때 this는...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.