swipe.js 898 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @description Swipes and moves an object.
  3. * @copyright 2014 by Tobias Reich
  4. */
  5. swipe = {
  6. obj: null,
  7. tolerance: 150,
  8. offset: 0
  9. }
  10. swipe.start = function(obj, tolerance) {
  11. if (obj) swipe.obj = obj;
  12. if (tolerance) swipe.tolerance = tolerance;
  13. return true;
  14. }
  15. swipe.move = function(e) {
  16. if (swipe.obj===null) return false;
  17. swipe.offset = -1 * e.x;
  18. swipe.obj.css({
  19. WebkitTransform: 'translateX(' + swipe.offset + 'px)',
  20. MozTransform: 'translateX(' + swipe.offset + 'px)',
  21. transform: 'translateX(' + swipe.offset + 'px)'
  22. });
  23. }
  24. swipe.stop = function(e, left, right) {
  25. if (e.x<=-swipe.tolerance) left(true);
  26. else if (e.x>=swipe.tolerance) right(true);
  27. else if (swipe.obj!==null) {
  28. swipe.obj.css({
  29. WebkitTransform: 'translateX(0px)',
  30. MozTransform: 'translateX(0px)',
  31. transform: 'translateX(0px)'
  32. });
  33. }
  34. swipe.obj = null;
  35. swipe.offset = 0;
  36. }