Advertisement
Guest User

swipes.js

a guest
Aug 3rd, 2012
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($) {
  2.   $.fn.swipeEvents = function() {
  3.     return this.each(function() {
  4.      
  5.       var startX,
  6.           startY,
  7.           $this = $(this);
  8.      
  9.       $this.bind('touchstart', touchstart);
  10.      
  11.       function touchstart(event) {
  12.         var touches = event.originalEvent.touches;
  13.         if (touches && touches.length) {
  14.           startX = touches[0].pageX;
  15.           startY = touches[0].pageY;
  16.           $this.bind('touchmove', touchmove);
  17.         }
  18.         event.preventDefault();
  19.       }
  20.      
  21.       function touchmove(event) {
  22.         var touches = event.originalEvent.touches;
  23.         if (touches && touches.length) {
  24.           var deltaX = startX - touches[0].pageX;
  25.           var deltaY = startY - touches[0].pageY;
  26.          
  27.           if (deltaX >= 50) {
  28.             $this.trigger("swipeLeft");
  29.           }
  30.           if (deltaX <= -50) {
  31.             $this.trigger("swipeRight");
  32.           }
  33.           if (deltaY >= 50) {
  34.             $this.trigger("swipeUp");
  35.           }
  36.           if (deltaY <= -50) {
  37.             $this.trigger("swipeDown");
  38.           }
  39.           if (Math.abs(deltaX) >= 50 || Math.abs(deltaY) >= 50) {
  40.             $this.unbind('touchmove', touchmove);
  41.           }
  42.         }
  43.         event.preventDefault();
  44.       }
  45.      
  46.     });
  47.   };
  48. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement