Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Port code from Google Fast Button article into jQuery special event

commit 6d0683a2277cd835b7c30c0a2dc0492bcefd59b7 0 parents
Matt Kelly authored August 26, 2011

Showing 1 changed file with 78 additions and 0 deletions. Show diff stats Hide diff stats

  1. 78  jquery.fastclick.js
78  jquery.fastclick.js
... ...
@@ -0,0 +1,78 @@
  1
+/*!
  2
+ * jQuery fastclick Plugin
  3
+ * http://www.zurb.com/playground/
  4
+ * Ported from Ryan Fioravanti's fast button article
  5
+ * http://code.google.com/mobile/articles/fast_buttons.html
  6
+ *
  7
+ * Copyright 2011, ZURB
  8
+ * Released under the MIT License
  9
+ */
  10
+(function ($) {
  11
+  
  12
+  $.event.special.fastclick = {
  13
+
  14
+		add: function (handleObj) {
  15
+		  var $this = $(this),
  16
+          startX,
  17
+          startY,
  18
+          coordinates = [];
  19
+		  
  20
+		  function onTouchStart(event) {
  21
+        event.stopPropagation();
  22
+
  23
+        $(this).bind('touchend', onClick);
  24
+        $('body').bind('touchmove', onTouchMove);
  25
+
  26
+        startX = event.originalEvent.touches[0].clientX;
  27
+        startY = event.originalEvent.touches[0].clientY;
  28
+      }
  29
+      
  30
+      function pop() {
  31
+        coordinates.splice(0, 2);
  32
+      }
  33
+
  34
+      function preventGhostClick(x, y) {
  35
+        coordinates.push(x, y);
  36
+        window.setTimeout(pop, 2500);
  37
+      }
  38
+
  39
+      function onClick(event) {
  40
+        event.stopPropagation();
  41
+        reset();
  42
+        handleObj.handler.apply(this, arguments);
  43
+
  44
+        if (event.originalEvent.type == 'touchend') {
  45
+          preventGhostClick(startX, startY);
  46
+        }
  47
+      }
  48
+
  49
+      function onTouchMove(event) {
  50
+        if (Math.abs(event.originalEvent.touches[0].clientX - startX) > 10 ||
  51
+            Math.abs(event.originalEvent.touches[0].clientY - startY) > 10) {
  52
+          reset();
  53
+        }
  54
+      }
  55
+
  56
+      function reset() {
  57
+        $this.unbind('touchend');
  58
+        $('body').unbind('touchmove');
  59
+      }
  60
+      
  61
+      function clickBusterOnClick(event) {
  62
+        for (var i = 0; i < coordinates.length; i += 2) {
  63
+          var x = coordinates[i];
  64
+          var y = coordinates[i + 1];
  65
+          if (Math.abs(event.originalEvent.clientX - x) < 25 && Math.abs(event.originalEvent.clientY - y) < 25) {
  66
+            event.stopPropagation();
  67
+            event.preventDefault();
  68
+          }
  69
+        }
  70
+      }
  71
+		  
  72
+		  $this.bind('touchstart', onTouchStart);
  73
+      $this.bind('click', onClick);
  74
+		  $(document).bind('click', clickBusterOnClick);
  75
+		}
  76
+	};
  77
+
  78
+}(jQuery));

0 notes on commit 6d0683a

Please sign in to comment.
Something went wrong with that request. Please try again.