swipe.js 946 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. }
  12. swipe.start = function(obj, tolerance) {
  13. if (obj) swipe.obj = obj;
  14. if (tolerance) swipe.tolerance = tolerance;
  15. return true;
  16. }
  17. swipe.move = function(e) {
  18. if (swipe.obj===null) return false;
  19. swipe.offset = -1 * e.x;
  20. swipe.obj.css({
  21. WebkitTransform: 'translateX(' + swipe.offset + 'px)',
  22. MozTransform: 'translateX(' + swipe.offset + 'px)',
  23. transform: 'translateX(' + swipe.offset + 'px)'
  24. });
  25. }
  26. swipe.stop = function(e, left, right) {
  27. if (e.x<=-swipe.tolerance) left(true);
  28. else if (e.x>=swipe.tolerance) right(true);
  29. else if (swipe.obj!==null) {
  30. swipe.obj.css({
  31. WebkitTransform: 'translateX(0px)',
  32. MozTransform: 'translateX(0px)',
  33. transform: 'translateX(0px)'
  34. });
  35. }
  36. swipe.obj = null;
  37. swipe.offset = 0;
  38. }