Determination of the sliding direction of the moving side touch

Source: Internet
Author: User

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;&AMP;&AMP;&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;&GT;&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.