swipe.js 946 B

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