lychee/src/scripts/swipe.js

52 lines
898 B
JavaScript
Raw Normal View History

2014-03-26 22:02:45 +00:00
/**
* @description Swipes and moves an object.
* @copyright 2014 by Tobias Reich
*/
swipe = {
2014-10-18 16:13:45 +00:00
obj: null,
tolerance: 150,
offset: 0
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
}
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
swipe.start = function(obj, tolerance) {
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
if (obj) swipe.obj = obj;
if (tolerance) swipe.tolerance = tolerance;
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
return true;
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
}
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
swipe.move = function(e) {
2014-10-18 16:13:45 +00:00
if (swipe.obj===null) return false;
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
swipe.offset = -1 * e.x;
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
swipe.obj.css({
WebkitTransform: 'translateX(' + swipe.offset + 'px)',
MozTransform: 'translateX(' + swipe.offset + 'px)',
transform: 'translateX(' + swipe.offset + 'px)'
});
2014-03-26 22:36:50 +00:00
2014-10-18 16:13:45 +00:00
}
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
swipe.stop = function(e, left, right) {
2014-03-26 22:02:45 +00:00
2014-10-18 16:13:45 +00:00
if (e.x<=-swipe.tolerance) left(true);
else if (e.x>=swipe.tolerance) right(true);
else if (swipe.obj!==null) {
swipe.obj.css({
WebkitTransform: 'translateX(0px)',
MozTransform: 'translateX(0px)',
transform: 'translateX(0px)'
});
2014-03-26 22:02:45 +00:00
}
2014-10-18 16:13:45 +00:00
swipe.obj = null;
swipe.offset = 0;
}