Advertisement
Guest User

swipe.js

a guest
Aug 3rd, 2012
2,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. * jQuery Mobile Framework 1.1.0 db342b1f315c282692791aa870455901fdb46a55
  3. * http://jquerymobile.com
  4. *
  5. * Copyright 2011 (c) jQuery Project
  6. * Dual licensed under the MIT or GPL Version 2 licenses.
  7. * http://jquery.org/license
  8. *
  9. */
  10.  
  11. /*
  12. * Stripped the touch swipe logic from jQuery Mobile and turned it into this plugin
  13. * Copyright 2012 (c) CodingJack - http://codecanyon.net/user/CodingJack
  14. * Dual licensed under the MIT or GPL Version 2 licenses.
  15. */
  16.  
  17. /* USAGE
  18.  
  19. // listen both left and right signals, the String "left" or "right" will be passed as an argument to the callback
  20. * $(element).touchSwipe(callback);
  21.  
  22. // second parameter is optional and will invoke "event.stopImmediatePropagation()"
  23. // use this if you need to prevent other mouse events from firing on the same object when a swipe gesture is detected
  24. * $(element).touchSwipe(callback, true);
  25.  
  26. // listen for only the left swipe event
  27. * $(element).touchSwipeLeft(callback);
  28.  
  29. // listen for only the right swipe event
  30. * $(element).touchSwipeRight(callback);
  31.  
  32. // unbind both left and right swipe events
  33. * $(element).unbindSwipe();
  34.  
  35. // unbind only left swipe event
  36. * $(element).unbindSwipeLeft();
  37.  
  38. // unbind only right swipe event
  39. * $(element).unbindSwipeRight();
  40.  
  41.  
  42. // SPECIAL NOTES
  43. * all methods return "this" for chaining
  44. * before a plugin event is added, "unbind" is called first to make sure events are never erroneously duplicated
  45.  
  46. */
  47.  
  48. ;(function($) {
  49.  
  50.   var touchStopEvent, touchMoveEvent, touchStartEvent,
  51.   horizontalDistanceThreshold = 30,
  52.   verticalDistanceThreshold = 75,
  53.   scrollSupressionThreshold = 10,
  54.   durationThreshold = 1000;
  55.  
  56.   if("ontouchend" in document) {
  57.  
  58.     touchStopEvent = "touchend.cj_swp";
  59.     touchMoveEvent = "touchmove.cj_swp";
  60.     touchStartEvent = "touchstart.cj_swp";
  61.    
  62.   }
  63.   else {
  64.  
  65.     touchStopEvent = "mouseup.cj_swp";
  66.     touchMoveEvent = "mousemove.cj_swp";
  67.     touchStartEvent = "mousedown.cj_swp";
  68.    
  69.   }
  70.  
  71.   $.fn.touchSwipe = function(cb, prevent) {
  72.    
  73.     if(prevent) this.data("stopPropagation", true);
  74.     if(cb) return this.each(swipeBoth, [cb]);
  75.    
  76.   }
  77.  
  78.   $.fn.touchSwipeLeft = function(cb, prevent) {
  79.    
  80.     if(prevent) this.data("stopPropagation", true);
  81.     if(cb) return this.each(swipeLeft , [cb]);
  82.    
  83.   }
  84.  
  85.   $.fn.touchSwipeRight = function(cb, prevent) {
  86.    
  87.     if(prevent) this.data("stopPropagation", true);
  88.     if(cb) return this.each(swipeRight, [cb]);
  89.  
  90.   }
  91.  
  92.   function swipeBoth(cb) {
  93.    
  94.     $(this).touchSwipeLeft(cb).touchSwipeRight(cb);
  95.    
  96.   }
  97.  
  98.   function swipeLeft(cb) {
  99.    
  100.     var $this = $(this);
  101.    
  102.     if(!$this.data("swipeLeft")) $this.data("swipeLeft", cb);
  103.    
  104.     if(!$this.data("swipeRight")) addSwipe($this);
  105.    
  106.   }
  107.  
  108.   function swipeRight(cb) {
  109.  
  110.     var $this = $(this);
  111.    
  112.     if(!$this.data("swipeRight")) $this.data("swipeRight", cb);
  113.    
  114.     if(!$this.data("swipeLeft")) addSwipe($this);
  115.    
  116.   }
  117.  
  118.   $.fn.unbindSwipeLeft = function() {
  119.    
  120.     this.removeData("swipeLeft");
  121.    
  122.     if(!this.data("swipeRight")) this.unbindSwipe(true);
  123.    
  124.   }
  125.  
  126.   $.fn.unbindSwipeRight = function() {
  127.    
  128.     this.removeData("swipeRight");
  129.    
  130.     if(!this.data("swipeLeft")) this.unbindSwipe(true);
  131.    
  132.   }
  133.  
  134.   $.fn.unbindSwipe = function(changeData) {
  135.    
  136.     if(!changeData) this.removeData("swipeLeft swipeRight stopPropagation");
  137.    
  138.     return this.unbind(touchStartEvent + " " + touchMoveEvent + " " + touchStopEvent);
  139.    
  140.   }
  141.  
  142.   function addSwipe($this) {
  143.    
  144.     $this.unbindSwipe(true).bind(touchStartEvent, touchStart);
  145.    
  146.   }
  147.  
  148.   function touchStart(event) {
  149.    
  150.     var time = new Date().getTime(),
  151.     data = event.originalEvent.touches ? event.originalEvent.touches[0] : event,
  152.     $this = $(this).bind(touchMoveEvent, moveHandler).one(touchStopEvent, touchEnded),
  153.     pageX = data.pageX,
  154.     pageY = data.pageY,
  155.     newPageX,
  156.     newPageY,
  157.     newTime;
  158.    
  159.     if($this.data("stopPropagation")) event.stopImmediatePropagation();
  160.      
  161.     function touchEnded(event) {
  162.      
  163.       $this.unbind(touchMoveEvent);
  164.  
  165.       if(time && newTime) {
  166.        
  167.         if(newTime - time < durationThreshold && Math.abs(pageX - newPageX) > horizontalDistanceThreshold && Math.abs(pageY - newPageY) < verticalDistanceThreshold) {
  168.          
  169.           if(pageX > newPageX) {
  170.            
  171.             if($this.data("swipeLeft")) $this.data("swipeLeft")("left");
  172.            
  173.           }
  174.           else {
  175.            
  176.             if($this.data("swipeRight")) $this.data("swipeRight")("right");
  177.            
  178.           }
  179.        
  180.         }
  181.        
  182.       }
  183.      
  184.       time = newTime = null;
  185.      
  186.     }
  187.    
  188.     function moveHandler(event) {
  189.  
  190.       if(time) {
  191.  
  192.         data = event.originalEvent.touches ? event.originalEvent.touches[0] : event;
  193.         newTime = new Date().getTime();
  194.         newPageX = data.pageX;
  195.         newPageY = data.pageY;
  196.  
  197.         if(Math.abs(pageX - newPageX) > scrollSupressionThreshold) event.preventDefault();
  198.        
  199.       }
  200.      
  201.     }
  202.    
  203.   }
  204.  
  205.    
  206. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement