First, touch events on the phone
Basic events:
Touchstart//trigger when finger just touches the screen
Touchmove//trigger when the finger moves on the screen
Touchend//trigger when the finger moves away from the screen
Here's less of a comparison:
Trigger when touchcancel//touch process is canceled by the system
Each event has the following list, such as Touchend's targettouches of course 0:
Touches//List of all fingers on the screen
Targettouches//List of all fingers located on this element
Changedtouches//List of all fingers involved in the current event
Each event has a list, and each list has the following properties:
Copy Code
Where coordinates are commonly used Pagex,pagey:
PageX//Relative to the X-coordinate of the page
Pagey//Relative to the Y-coordinate of the page
ClientX//X-coordinate relative to viewport
ClientY//Y coordinate relative to the viewport
ScreenX//Relative to the X-coordinate of the screen
ScreenY//Relative to the Y-coordinate of the screen
Identifier//Unique number of the current touch point
Target//The DOM element that the finger touches
Copy Code
Other related events:
Event.preventdefault ()//block browser zoom, scroll bar scrolling when touch is blocked
var Supporttouch = "Createtouch" in document//To determine if touch events are supported
More in-depth content? Click: http://www.cesclub.com/bw/jishuzhongxin/Webjishu/2011/1216/18069.html
Ii. examples
The following is to obtain different types of sliding code specific practices, combined with previous ideas, encapsulated well, can learn from learning:
Copy Code
var touchfunc = function (Obj,type,func) {
Sliding range within 5x5 do click Processing, S is the start, E is the end
var init = {x:5,y:5,sx:0,sy:0,ex:0,ey:0};
var stime = 0, etime = 0;
Type = Type.tolowercase ();
Obj.addeventlistener ("Touchstart", function () {
stime = new Date (). GetTime ();
INIT.SX = Event.targettouches[0].pagex;
Init.sy = Event.targettouches[0].pagey;
Init.ex = INIT.SX;
Init.ey = Init.sy;
if (Type.indexof ("start")! =-1) func ();
}, False);
Obj.addeventlistener ("Touchmove", function () {
Event.preventdefault ()///block browser zoom, scroll bar scrolling when blocking touch
Init.ex = Event.targettouches[0].pagex;
Init.ey = Event.targettouches[0].pagey;
if (Type.indexof ("move")!=-1) func ();
}, False);
Obj.addeventlistener ("Touchend", function () {
var changex = Init.sx-init.ex;
var changey = Init.sy-init.ey;
if (Math.Abs (Changex) >math.abs (changey) &&math.abs (changey) >init.y) {
Left and right events
if (Changex > 0) {
if (Type.indexof ("left")!=-1) func ();
}else{
if (Type.indexof ("right")!=-1) func ();
}
}
else if (Math.Abs (changey) >math.abs (Changex) &&math.abs (Changex) >init.x) {
Up and down events
if (Changey > 0) {
if (Type.indexof ("top")!=-1) func ();
}else{
if (Type.indexof ("Down")!=-1) func ();
}
}
else if (Math.Abs (Changex) <init.x && math.abs (changey) <init.y) {
ETime = new Date (). GetTime ();
Click events, which are broken down by time difference
if ((Etime-stime) > 300) {
if (Type.indexof ("Long")!=-1) func (); Long Press
}
else {
if (Type.indexof ("click")!=-1) func (); When you click Process
}
}
if (Type.indexof ("End")!=-1) func ();
}, False);
};
Mobile-side JS Touch Event Basics