Basic ideas:
1) Detect finger slide direction: Get the position when the finger is lifted, minus the position when the finger is pressed, it is going to slide.
2) When the finger is lifted, change the position of the current page to the corresponding reverse operation
The specific code is as follows:
Html
<div id= "wrap" > <div id= "page01" class= "pages" > First screen </div> <div id= "page02" class= "pages "> Second screen </div> <div id=" page03 "class=" pages "> Third screen </div> <div id=" page04 "class=" pages "> Fourth screen </div></div><div id=" dots "> <span class=" Now "></span><span class=" " ></span><span class= "" ></span><span class= "" ></span></div>
Css
*{margin:0;padding:0; }Body{Overflow:Hidden; }#wrap > Div{width:10rem;position:Absolute; Left:0;Top:0;background:#fff;transition:All 0.3s Ease; }#dots{position:fixed; Right:5px;Top:40%;Z-index:9; }#dots span{Display:Block;Height:0.2rem;width:0.2rem;Border:1px solid #000;Border-radius:50%;Margin-bottom:3px; }#dots. now{background:#555; }
JS is divided into two pieces
First, set the font-size of the HTML tag to set the base of REM (placed on the page header)
document.getElementsByTagName ("HTML") [0].style.fontsize = Window.innerwidth/10 + "px";
Second, the specific sliding operation code
Window.onload =function(){ varOdiv = document.getElementById ("Wrap"); varApages = Odiv.getelementsbyclassname ("pages"); varAdots = document.getElementById ("dots"). getElementsByTagName ("span"); varWinh =Window.innerheight; varTtime = 1; //set the height and ZIndex values per page for(vari=0; i<apages.length; i++) {Apages[i].style.height= Winh + "px"; Apages[i].style.zindex= 1; } apages[0].style.zindex = 3; apages[1].style.zindex = 2; ODiv.style.height= Winh + "px"; //Finger Drag events (remove default actions)Document.addeventlistener ("Touchmove",function(e) {e.preventdefault (); }); varYstart = 0; varInow = 0; //Finger PressOdiv.addeventlistener ("Touchstart",function(e) {Ystart= E.changedtouches[0].clienty; }); //Finger MovementOdiv.addeventlistener ("Touchmove",function(e) {Disy= E.changedtouches[0].clienty-ystart;//slide positive, swipe up negative }); //Finger awayOdiv.addeventlistener ("Touchend",function(e) {Disy= E.changedtouches[0].clienty-ystart;//slide positive, swipe up negative if(Math.Abs (Disy) >winh/20) {//switch only when the sliding distance is greater than it must beif(disy<0) {Inow++; if(inow>=adots.length) {Inow= 0; } apages[0].style.transform = "Translatey (" +-winh + "px)"; Doslide (); }Else{Inow--; if(inow<0) {Inow= Adots.length-1; } apages[0].style.transform = "Translatey (" + Winh + "px)"; Doslide ("Up"); } } }); functionDoslide (upflag) { for(vari=0;i<adots.length;i++) {Adots[i].classname= ""; } adots[inow].classname= "Now"; if(Upflag) {//Slide upapages[3].style.zindex = 2; apages[1].style.zindex = 1; Odiv.insertbefore (apages[3],apages[1]); SetTimeout (function() {apages[1].style.transform = "Translatey (0px)"; apages[1].style.zindex = 2; apages[0].style.zindex = 3; },300) }Else{setTimeout (function() {apages[0].style.transform = "Translatey (0px)"; apages[0].style.zindex = 1; apages[1].style.zindex = 3; apages[2].style.zindex = 2; Odiv.appendchild (apages[0]); },300) } } }
The realization of sliding on the whole screen of mobile terminal