Skip to content

Instantly share code, notes, and snippets.

@wcccode
Created March 16, 2015 08:56
Show Gist options
  • Save wcccode/00279ce48d91eab88e8f to your computer and use it in GitHub Desktop.
Save wcccode/00279ce48d91eab88e8f to your computer and use it in GitHub Desktop.
手机滑动
function Swap(opt){
var b = this.extend(this.defaultOpt, this.extend({} , opt || {}) ),
obj = document.getElementById(b.targetId);
b.touchAble = ("ontouchstart" in window) || window.DocumentTouch && document instanceof DocumentTouch,
b.obj = obj;
function start(e){
e = e ? e : event,
e.targetTouches ? (b.startX = e.targetTouches[0].clientX, b.startY = e.targetTouches[0].clientY) :
(b.startX = e.clientX, b.startY = e.clientY);
}
function move(e){
e = e ? e : event,
e.targetTouches ? (b.dx = e.targetTouches[0].clientX, b.dy = e.targetTouches[0].clientY) :
(b.dx = e.clientX, b.dy = e.clientY),
b.bx = b.startX - b.dx,
b.by = b.startY - b.dy,
b.isMove = !0,
b.horizontal = Math.abs(b.bx) > Math.abs(b.by) ? !0 : !1,
b.orientationX = (b.bx > 0) ? !0 : !1,
b.orientationY = (b.by > 0) ? !0 : !1,
b.horizontal ? (e.preventDefault(), b.isHorizontal = !0, b.orientation = b.orientationX ? "left" : "right") : (b.isHorizontal = !1 , b.orientation = b.orientationY ? "up" : "down");
b.moving(b);
}
function end(e){
b.isMove !== !0 ? (b.orientation = "point", b.horizontal = b.isHorizontal = b.orientationX = b.orientationY = void 0) : void 0,
b.moveEnd(b),
b.isMove = void 0
}
return {
init:function(){
try{
if(obj != null){
obj.addEventListener("touchstart",start,false);
obj.addEventListener("touchmove",move,false);
obj.addEventListener("touchend",end,false);
}else{
throw new Error("Element id is not exist---Id="+b.targetId);
}
}catch(e){
//alert(e);
//throw e;
}
}
}
};
Swap.prototype = {
extend : function(t,s){
for(var p in s){
t[p] = s[p];
}
return t;
},
defaultOpt : {
targetId: null,
obj: null,
startX: 0, //开始x坐标
startY: 0, //开始y坐标
isHorizontal: !1, //是否水平滑动
touchAble: !1, //可触屏
isMove: !1,
isEnd: !0,
bx: 0,//x距离
by: 0,//y距离
dx: 0,//移动x坐标
dy: 0,//移动x坐标
navId: "",
navClass: "",
navSelectClass: "",
elemTag: "",
moveStart: function(){},
moving: function(){},
moveEnd: function(){}
}
}
//滑动
var swap = new Swap({
targetId : "choice-chexian-list",
minLeft : -1000,
maxRight : 1000,
stepLength : 1,
left : $("#choice-chexian-list").css("left").replace("px","").replace("%",""),
moving : function(a){
//a.isHorizontal === !0 ? moving(a) : void 0;
},
moveEnd : function(a){
$("#choice-chexian-list").css("left","50%");
a.orientation === "right" ? function(){} : (a.orientation === "left" ? function(){} : void 0);
}
});
swap.init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment