<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title></title>
<script type="text/javascript">
window.onload = function() {
document.addEventListener("touchstart",touchStart, false);
document.addEventListener("touchend",touchEnd, false);
document.addEventListener("touchmove",touchMove, false);
}
function touchStart(event) {
Ev= event || window.event;
var touchPos = touchCoords(Ev);
var eventx = document.getElementById("eventx");
eventx.value = touchPos.x;
var eventy = document.getElementById("eventy");
eventy.value = touchPos.y;
var textmsg = document.getElementById("eventmsg");
textmsg.value = "down";
}
function touchEnd(event) {
var textmsg = document.getElementById("eventmsg");
textmsg.value = "up";
}
function touchMove(event) {
Ev= event || window.event;
var touchPos = touchCoords(Ev);
var eventx = document.getElementById("eventx");
eventx.value = touchPos.x;
var eventy = document.getElementById("eventy");
eventy.value = touchPos.y;
var textmsg = document.getElementById("eventmsg");
textmsg.value = "move";
}
function touchCoords(ev)
{
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return{
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
</script>
</head>
<body>
<table>
<tr>
<td>
<table>
<tr>
<td><input type="text" id="eventmsg" size=10></input></td>
<td><input type="text" id="eventx" size=10></input></td>
<td><input type="text" id="eventy" size=10></input></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>
Javascript touch事件实现
最新推荐文章于 2022-05-21 16:24:33 发布