swipe.js 945 B

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