var slider = { //determine if the device supports touch event touch: (' Ontouchstart ' in window) | | window. documenttouch && document instanceof documenttouch, //Event events:{ slider:document, handleevent:function (event) { if (event.type == ' Touchstart ') { this.start (event); }else if (event.type == ' Touchmove ') { this.move (event); }else if (event.type == ' Touchend ') { this.end (event); } }, //Slide Start start:function (event) { //touches Array object get all the touch on the screen, take the first touch var touch = event.targettouches[0]; //take the first touch coordinate value startpos = {x:touch.pagex,y:touch.pagey,time:+new date}; //This parameterDetermines whether vertical scrolling or horizontal scrolling isscrolling = 0; this.slider.addeventlistener (' Touchmove ', this,false); this.slider.addeventlistener (' Touchend ', this,false); }, //Mobile move:function (event) { //When the screen has more than one touch or the page is being retracted, No move operation is performed if (event.targettouches.length > 1 | | &NBSP;EVENT.SCALE&NBSP;&&&NBSP;EVENT.SCALE&NBSP;!==&NBSP;1) return; var touch = event.targettouches[0]; endpos = {x:touch.pagex - Startpos.x,y:touch.pagey - startpos.y}; //isscrolling for 1 o'clock, for portrait slide, 0 for lateral slide isscrolling = math.abs (endpos.x) < math.abs (ENDPOS.Y) ? 1:0; if (isscrolling === 1) { //block the default behavior of touch events, that is, prevent scrolling event.preventdefault (); } }, //Slide Release end:function (Event) { //duration of sliding var duration = +new date - startpos.time; var i = 0; if (number (duration) > 10) { if (isscrolling === 1) { //Determines whether to move up or down when the offset is greater than 10 o'clock to execute if (endpos.y < &NBSP;-10) { i = 1; }else if ( ENDPOS.Y&NBSP;>&NBSP;10) { i = 3; } }else if (isscrolling === 0) { //Determines whether to move left or right, Execute if (endpos.x > 10) { i When offset is greater than 10 o'clock = 2; }else if (endpos.x < -10) { i = 4; } } this.callback (i); startPos = endPos = null; return false; } //Unbind Event this.slider.removeeventlistener (' Touchmove ', this,false); this.slider.removeeventlistener (' Touchend ', This,false); }, //to execute function callback:function (direction) { //upper right left 1234 switch (direction) { case 1: alert (' on '); break; case 2: alert (' Right '); break; case 3: alert (' Down '); break; case 4: Alert (' left '); break; default: break; }; }, }, //Initialize init:function () { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF (!! This.touch) this.events.slider.addeventlistener (' Touchstart ', this.events,false); } };slider.init ();
If there is a need to modify the place or there are other better ways to ask the great God a lot of advice, thank you!
Determination of the sliding direction of the moving side touch