window.touchMove=function(){
// 用于紀錄觸摸開始時的坐标
var startX=0,startY=0,
//建立一個變量,用于儲存觸摸方向
touchDirection="";
//建立一個對象,用于儲存滑動事件
var funcs = {};
if(arguments.length>=2&&arguments.length%2==0){
for(var i=0;i<arguments.length;i+=2){
funcs[arguments[i]]=arguments[i+1];
}
var elem =$("#wrapper")[0];
var style = window.getComputedStyle ? window.getComputedStyle(elem,null) : null || elem.currentStyle;
//主程式事件,用于給document綁定觸摸事件
document.addEventListener('touchstart',touchSatrtFunc, false);
document.addEventListener('touchmove',touchMoveFunc, false);
document.addEventListener('touchend', touchEndFunc, false);
//定義觸摸開始方法,擷取觸摸事件起始坐标
function touchSatrtFunc(e){
e.stopPropagation();
touchDirection="";
// e.preventDefault();
var touch=e.touches[0];
startX=touch.pageX;
startY=touch.pageY;
//慣性滑動的處理,讓滑動變得流暢
if(funcs.up!==undefined&&elem.offsetHeight>=document.body.clientHeight){
startTopScroll = elem.scrollTop;
//當滾動條在最頂部的時候
if(startTopScroll <= 0)
elem.scrollTop = 1;
//當滾動條在最底部的時候
if(startTopScroll + elem.offsetHeight >= elem.scrollHeight)
elem.scrollTop = elem.scrollHeight - elem.offsetHeight - 1;
}
// else
// e.preventDefault();
};
//定義觸摸移動時的方法,擷取其坐标并調用判斷觸摸方向的方法
function touchMoveFunc(e){
e.stopPropagation();
var touch = e.touches[0];
//調用判斷觸摸方向的方法
touchDirectionFunc(touch.pageX,touch.pageY,e);
};
//判斷觸摸方向的方法
function touchDirectionFunc(x,y,e){
var moveDirection =Math.abs(y-startY)/Math.abs(x-startX);
if(y-startY<-10&&moveDirection>=1.5) {
touchDirection="up";
}
else if(y-startY>10&&moveDirection>=1.5) {
touchDirection="down";
}
else if(x-startX<-10&&moveDirection<=0.5) {
touchDirection="left";
e.preventDefault();
// 橫向滑動時阻止上下滑動
}
else if(x-startX>10&&moveDirection<=0.5) {
touchDirection="right";
e.preventDefault();
// 橫向滑動時阻止上下滑動
}
// else{
// e.preventDefault();
// }
};
function touchEndFunc(e) {
e.stopPropagation();
//調用上拉事件
if(touchDirection=="up"&&funcs.up!==undefined){
funcs.up(e.target);
}
//調用下拉事件
else if(touchDirection=="down"&&funcs.down!==undefined){
funcs.down(e.target);
}
// 調用左滑事件
else if(touchDirection=="left"&&funcs.left!==undefined){
funcs.left(e.target);
}
//調用右滑事件
else if(touchDirection=="right"&&funcs.right!==undefined){
funcs.right(e.target);
}
}
}
}
// 調用上拉,左滑,右滑方法
touchMove("up",addMore,"left",moveLeft,"right",moveRight);