varEle = Document.getelementsbyclassname ("Img-box") [0]; varBeginX, BeginY, EndX, EndY, Swipeleft, swiperight; Ele.addeventlistener (' Touchstart ',function(event) {event.stoppropagation (); Event.preventdefault (); BeginX= Event.targettouches[0].screenx; BeginY= Event.targettouches[0].screeny; Swipeleft=false, swiperight =false; }); Ele.addeventlistener (' Touchmove ',function(event) {event.stoppropagation (); Event.preventdefault (); EndX= Event.targettouches[0].screenx; EndY= Event.targettouches[0].screeny; //Swipe left and right if(Math.Abs (Endx-beginx)-Math.Abs (Endy-beginy) > 0) { /*Swipe Right*/ if(Endx-beginx > 0) {swiperight=true; Swipeleft=false; } /*Swipe left*/ Else{swipeleft=true; Swiperight=false; } } Else if(Math.Abs (Endx-beginx)-Math.Abs (Endy-beginy) < 0) { //swipe up and down } }); Ele.addeventlistener (' Touchend ',function(event) {event.stoppropagation (); Event.preventdefault (); if(Math.Abs (Endx-beginx)-Math.Abs (Endy-beginy) > 0) {event.stoppropagation (); Event.preventdefault ();if(swiperight) {swiperight= !swiperight; / * swipe right * / } if(swipeleft) {swipeleft= !swipeleft; / * swipe left * / } } });
In addition to this approach you can use third-party plug-ins to achieve the same effect, such as QUOJS contains a variety of mobile gesture effects and AJAX requests and other operations, but in the use of the process encountered problems. After using Quojs, some of the jquery's click events and some of the A-tag href properties are invalidated. and found Quojs in the Swipeleft and swiperight events, the finger can only slide horizontally, if a little tilt a little sliding, then the event does not take effect, do not know whether it is my device problem.
JS to achieve the mobile end of the finger left and right swipe page effect