您的位置:首页 > Web前端 > JavaScript

Javascript touch事件实现

2012-03-13 20:28 330 查看
<!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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: