毫无保留开源我写的:IOS Android Ipad 多点触摸通用js 库

毫无保留开源我写的:IOS Android Ipad 多点触摸通用js 库

在线演示地址: http://m.yunxunmi.com/
支持 IOS Android Ipad 等不同操作系统的手持或平板设备

代码如下:
一、index.html
<html>
<title>手指触摸事件</title>
<head>
<style type="text/css">
#touchable
{
height: 75px;
width: 197px;
background-color: #0033CC;
color: #FFCC66;
text-align: center;
}
#output
{
height: 200px;
width: 200px;
}
</style>
</head>
<script src="javascript/hzTouch.js" type="text/javascript"></script>
<body>
<div id='touchable' class="style2">
<b>
<br />
<span class="style1">
<br />
用来判断上下移动</span></b>
</div>

<br/>
<textarea id="output" >
</textarea>

<script>

//触摸后回调方法
//第一触点开始X坐标,第一触点开始Y坐标,第一触点结束X坐标,第一触点结束Y坐标,
//第二触点开始X坐标,第二触点开始Y坐标,第二触点结束X坐标,第二触点结束Y坐标,
//触摸类型,触摸值(如果是移动就是移动距离,如果是缩放则是缩放比)
function myCallBack(FirstStartX, FirstStartY, FirstEndX, FirstEndY,
SecondStartX, SecondStartY, SecondEndX, SecondEndY,
mTouchType, TouchValue) {
var output = document.getElementById("output");
output.innerHTML = "第一触点开始:" + FirstStartX + ":" + FirstStartY + "\n";
output.innerHTML += "第一触点结束:" + FirstEndY + ":" + FirstEndY + "\n";
switch (mTouchType) {
case TouchType.Up:
output.innerHTML += "向上移动了:" + TouchValue + "\n";
break;
case TouchType.Down:
output.innerHTML += "向下移动了:" + TouchValue + "\n";
break;
case TouchType.Left:
output.innerHTML += "向左移动了:" + TouchValue + "\n";
break;
case TouchType.Right:
output.innerHTML += "向右移动了:" + TouchValue + "\n";
break;
case TouchType.Zoom:
output.innerHTML += "第二触点开始:" + SecondStartX + ":" + SecondStartY + "\n";
output.innerHTML += "第二触点结束:" + SecondEndX + ":" + SecondEndY + "\n";
output.innerHTML += "缩放比例是:" + TouchValue + "\n";
break;
default:
break;
}
}
//创建指定DOM对象的触摸对象
var testTouch = new YXMTouch('touchable', 200, "output", false, myCallBack);

</script>

</body>
</html>

二、hzTouch.js
var meta = document.createElement('meta');
meta.setAttribute('name', 'HandheldFriendly');
meta.setAttribute('content', 'True');
document.getElementsByTagName('head')[0].appendChild(meta);

meta = document.createElement('meta');
meta.setAttribute('name', 'viewport');
meta.setAttribute('content', 'width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;');
document.getElementsByTagName('head')[0].appendChild(meta);

meta = document.createElement('meta');
meta.setAttribute('name', 'viewport');
meta.setAttribute('content', 'width=device-width');
document.getElementsByTagName('head')[0].appendChild(meta);

var TouchType =
{
Up: 1,
Down: 2,
Left: 3,
Right: 4,
Zoom: 5
}

function documentTouchEvent(event) {
//只跟踪一次触摸
if (event.touches.length == 1) {
switch (event.type) {
case "touchstart":
event.preventDefault(); //阻止滚动
break;
case "touchend":
event.preventDefault(); //阻止滚动
break;
case "touchmove":
event.preventDefault(); //阻止滚动
break;
}
}
}

document.addEventListener("touchstart", documentTouchEvent, false);
document.addEventListener("touchend", documentTouchEvent, false);
document.addEventListener("touchmove", documentTouchEvent, false);

//计算两坐标直线距离
function CoordinateDistance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}

//计算缩放比
function Scaling(x1, y1, x2, y2, x3, y3, x4, y4) {
return (CoordinateDistance(x2, y2, x4, y4) / CoordinateDistance(x1, y1, x3, y3)).toFixed(2);
}

function YXMTouch(TouchObjId, MinDelay, LogObjId, IsLog, CallBack) {
var minTime = MinDelay;
var startTime = 0;
var SX = 0;
var SY = 0;
var EX = 0;
var EY = 0;
var SX1 = 0;
var SY1 = 0;
var EX1 = 0;
var EY1 = 0;
var output = document.getElementById(LogObjId);
var isLog = IsLog == null ? false : IsLog;

function handleTouchEvent(event) {
switch (event.type) {
case "touchstart":
SX = event.touches[0].clientX;
SY = event.touches[0].clientY;
if (this.isLog)
output.innerHTML = "触摸开始0 (" + SX + "," + SY + ")" + "\n";
try {
SX1 = event.touches[1].clientX;
SY1 = event.touches[1].clientY;
if (this.isLog)
output.innerHTML = "触摸开始1 (" + SX1 + "," + SY1 + ")" + "\n";
}
catch (err) { }
startTime = event.timeStamp;

break;
case "touchend":
startTime = 0;
if (this.isLog)
output.innerHTML += "触摸结束 (" + event.changedTouches[0].clientX + "," + event.changeTouches[0].clientY + ")" + "\n";
break;
case "touchmove":
var str = "";
event.preventDefault();
var currentTime = event.timeStamp;
if (startTime !== 0 && currentTime - startTime > minTime) {
EX = event.changedTouches[0].clientX;
EY = event.changedTouches[0].clientY;
try {
EX1 = event.touches[1].clientX;
EY1 = event.touches[1].clientY;
}
catch (err) { }
CallBackTouchEnd();
}
break;
}
}

function CallBackTouchEnd() {
startTime = 0;
startX = 0;
var mTouchType;
var mValue;

if ((SX1 !== 0) && (SY1 !== 0) && (EX1 !== 0) && (EY1 !== 0)) {
mTouchType = TouchType.Zoom;
mValue = Scaling(SX, SY, EX, EY, SX1, SY1, EX1, EY1);
}
else {
var DiffY = Math.abs(EY - SY);
var DiffX = Math.abs(EX - SX);
if (DiffY >= DiffX) {
mValue = DiffY;
if (EY >= SY)
mTouchType = TouchType.Down;
else
mTouchType = TouchType.Up;
}
else {
mValue = DiffX;
if (EX >= SX)
mTouchType = TouchType.Right;
else
mTouchType = TouchType.Left;
}
}

if (this.isLog) {
output.innerHTML += "开始:" + SX + ":" + SY + "\n";
output.innerHTML += "结束:" + EX + ":" + EY + "\n";
switch(mTouchType)
{
case TouchType.Up:
output.innerHTML += "向上移动了:" + mValue + "\n";
break;
case TouchType.Down:
output.innerHTML += "向下移动了:" + mValue + "\n";
break;
case TouchType.Left:
output.innerHTML += "向左移动了:" + mValue + "\n";
break;
case TouchType.Right:
output.innerHTML += "向右移动了:" + mValue + "\n";
break;
case TouchType.Zoom:
output.innerHTML += "开始1:" + SX1 + ":" + SY1 + "\n";
output.innerHTML += "结束1:" + EX1 + ":" + EY1 + "\n";
output.innerHTML += "缩放比例是:" + mValue + "\n";
break;
default:
break;
}
}

CallBack(SX, SY, EX, EY, SX1, SY1, EX1, EY1, mTouchType, mValue);
SX = 0; SY = 0; EX = 0; EY = 0;
SX1 = 0; SY1 = 0; EX1 = 0; EY1 = 0;
}

document.getElementById(TouchObjId).addEventListener("touchstart", handleTouchEvent, false);
document.getElementById(TouchObjId).addEventListener("touchmove", handleTouchEvent, false);
document.getElementById(TouchObjId).addEventListener("touchend", handleTouchEvent, false);
}

运行效果:

毫无保留开源我写的:IOS Android Ipad 多点触摸通用js 库

毫无保留开源我写的:IOS Android Ipad 多点触摸通用js 库

毫无保留开源我写的:IOS Android Ipad 多点触摸通用js 库

用手机访问看看
上面蓝色区域可以感知手指的多点触摸
支持 IOS IPAD Android

在线演示地址: http://m.yunxunmi.com/
支持 IOS Android Ipad 等不同操作系统的手持或平板设备

手机APP开发 QQ交流群:181978302

上一篇:HttpClient(二)HttpClient使用Ip代理与处理连接超时


下一篇:使用python遍历指定城市的一周气温