swipe.js 983 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @description Swipes and moves an object.
  3. * @copyright 2015 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. // Only execute once
  26. if (swipe.obj==null) return false
  27. if (e.x<=-swipe.tolerance) {
  28. left(true)
  29. } else if (e.x>=swipe.tolerance) {
  30. right(true)
  31. } else {
  32. swipe.obj.css({
  33. WebkitTransform : 'translateX(0px)',
  34. MozTransform : 'translateX(0px)',
  35. transform : 'translateX(0px)'
  36. })
  37. }
  38. swipe.obj = null
  39. swipe.offset = 0
  40. }