Mobile Touch Event Two

Source: Internet
Author: User

Now the smart phone is hot, we are all touch screen things, then we used to JS writing is unable to meet a number of requirements, I would like to introduce you to a few JS mobile phone touch Screen event usage

Handling Touch events allows you to track the location of each finger of the user. You can bind the following four kinds of touch events:

One

touchstart://when you put your finger on the screen.
touchmove://when the finger moves on the screen
touchend://when the finger picks up from the screen.
triggered when the touchcancel://system cancels the touch event. As to when the system will be canceled, unknown

Two

client/clienty://the location of the touch point relative to the browser window viewport
pagex/pagey://the position of the touch point relative to the page
screenx/screeny://the position of the touch point relative to the screen
Unique ID of the identifier:>//touch object

Three

Each Touch object contains the following properties.

ClientX: Touch the x-coordinate of the target in the viewport.
ClientY: Touch the y-coordinate of the target in the viewport.
Identifier: Represents the unique ID of the touch.
PageX: Touch the x-coordinate of the target in the page.
Pagey: Touch the y-coordinate of the target in the page.
ScreenX: Touch the x-coordinate of the target in the screen.
ScreenY: Touch the y-coordinate of the target in the screen.
Target: The coordinates of the DOM node of the touch.

Cases

The code is as follows Copy Code

<!doctype html>


<meta charset= "Utf-8" >
<title>touches</title>
<style>
body{font-size:60px; color:red;}
</style>
<script>
function touches (EV) {
if (ev.touches.length==1) {
var Odiv=document.getelementbyid (' Div1 ');
Switch (ev.type) {
Case ' Touchstart ':
Odiv.innerhtml= ' Touch start (' +ev.touches[0].clientx+ ', ' +ev.touches[0].clienty+ ') ';
Ev.preventdefault (); Prevent scroll bars from appearing
Break
Case ' Touchend ':
Odiv.innerhtml= ' Touch end (' +ev.changedtouches[0].clientx+ ', ' +ev.changedtouches[0].clienty+ ') ';
Break
Case ' Touchmove ':
Odiv.innerhtml= ' Touch move (' +ev.changedtouches[0].clientx+ ', ' +ev.changedtouches[0].clienty+ ') ';
Break

}
}
}
Document.addeventlistener (' Touchstart ', touches,false);
Document.addeventlistener (' Touchend ', touches,false);
Document.addeventlistener (' Touchmove ', touches,false);
</script>


<body>
<div id= "Div1" ></div>
</body>

Second, the gesture event:

Gesturestart: Triggers when one finger presses on the screen and the other finger has a trigger screen;

Gestureend: Triggers when any one of your fingers moves away from the screen;

Gesturechange: Triggers when any finger on the touch screen changes;

In the event object here, the others are still the same, there are two more useful things in the gesture:

One is rotation: the rotation angle caused by the change of the finger, clockwise is positive, the counter-clockwise is negative;

There is also a scale: the distance between two fingers changes;

Cases

The code is as follows Copy Code
<!doctype html>


<meta charset= "Utf-8" >
<title>gesture</title>
<style>
body{font-size:60px; color:red;}
</style>
<script>
Window.onload=function () {
function gesture (EV) {
var Div=document.getelementbyid (' Div1 ');
Switch (ev.type) {
Case ' Gesturestart ':
Div.innerhtml= ' Gesture start (rotation= ' +ev.rotation+ ', scale= ' +ev.scale+ ') ';
Ev.preventdefault ();
Break
Case ' Gestureend ':
Div.innerhtml= ' Gesture End (rotation= ' +ev.rotation+ ', scale= ' +ev.scale+ ') ';
Break
Case ' Gesturechange ':
Div.innerhtml= ' Gesture change (rotation= ' +ev.rotation+ ', scale= ' +ev.scale+ ') ';
Break
}
}
Document.addeventlistener (' Gesturestart ', gesture,false);
Document.addeventlistener (' Gestureend ', gesture,false);
Document.addeventlistener (' Gesturechange ', gesture,false);
}
</script>


<body>
<div id= "Div1" ></div>
</body>


Mobile Touch Event Two

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.