Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Documentation update

- Made inline comments more descriptive
- Re-ordered functions in swewsh.js to make more sense (pointless
because of hoisting, but it makes sense to me and it's my project, so
there.)
- added license information
- updated testing info in README.txt
  • Loading branch information...
commit f4a7253f738e7ea3ceb8d2e864556cc2ef43c9d3 1 parent b92df88
Scott Kosman authored
Showing with 44 additions and 41 deletions.
  1. +1 −1  README.txt
  2. +43 −40 lib/swewsh.js
View
2  README.txt
@@ -2,6 +2,6 @@ A small (2k unminified) javascript library that exists to add left/right swipe s
Still very much a work in progress, no warranties expressed or implied, may cause your website to break, your server to explode, or the heat death of the universe.
-Note: this has only been tested in iOS so far. Android WebKit, Opera mini, etc. testing to come as I can find emulators and devices to test on.
+Note: this has only been tested in iOS and a little bit in Android WebKit thus far. Opera Mini/Mobile, Firefox Mobile, WP7 etc. testing to come as I can find emulators and devices to test on.
Excelsior!
View
83 lib/swewsh.js
@@ -1,8 +1,11 @@
-/*jslint browser: true */
-/*global alert */
-
-// The previous two lines are for JSLintMate, the awesome TextMate JSLint bundle which you should totally check out.
-// https://github.com/rondevera/jslintmate
+/*!
+ * swewsh.js - adds swipe touch event support to any website.
+ * http://prayingmadness.com
+ *
+ * Copyright (c) 2011 Scott Kosman
+ * Dual licensed under the MIT and GPL licenses.
+ *
+ */
var Swewsh = {
x : null,
@@ -11,30 +14,26 @@ var Swewsh = {
x_threshold : 5,
y_threshold : 40,
callback : null,
-
- cancel : function() {
- //Remove event listeners for touchmove and touchend
- document.removeEventListener('touchmove', Swewsh.move);
- document.removeEventListener('touchend', Swewsh.end);
-
- //Reset variables
- Swewsh.x = null;
- Swewsh.y = null;
- Swewsh.dx = null;
+
+ init: function(event, callback) {
+ Swewsh.callback = callback;
+ document.addEventListener('touchstart', Swewsh.start, false);
},
-
- end : function(event) {
- if (Math.abs(Swewsh.dx) > Swewsh.x_threshold) {
- //Determine direction and call callback
- var direction = (Swewsh.dx > 0) ? 'right' : 'left';
- Swewsh.callback(direction);
+
+ start: function(event) {
+ //Fire only on single-finger touch, multi-touch gets ignored.
+ if (event.touches.length === 1) {
+ Swewsh.x = event.touches[0].pageX;
+ Swewsh.y = event.touches[0].pageY;
+
+ //Add events listeners for touchmove and touchend
+ document.addEventListener('touchmove', Swewsh.move, false);
+ document.addEventListener('touchend', Swewsh.end, false);
}
- //Reset swewsh
- Swewsh.cancel();
},
-
- move : function(event) {
- //Handle only for single touch
+
+ move: function(event) {
+ //Fire only on single-finger touch, multi-touch gets ignored.
if (event.touches.length === 1) {
var dx = event.touches[0].pageX - Swewsh.x;
var dy = event.touches[0].pageY - Swewsh.y;
@@ -50,31 +49,35 @@ var Swewsh = {
}
},
- start : function(event) {
- //Handle only for single touch
- if (event.touches.length === 1) {
- Swewsh.x = event.touches[0].pageX;
- Swewsh.y = event.touches[0].pageY;
-
- //Add events listeners for touchmove and touchend
- document.addEventListener('touchmove', Swewsh.move, false);
- document.addEventListener('touchend', Swewsh.end, false);
+ end: function(event) {
+ if (Math.abs(Swewsh.dx) > Swewsh.x_threshold) {
+ //Determine direction and fire callback, sending the diretion name to the callback as an argument
+ var direction = (Swewsh.dx > 0) ? 'right' : 'left';
+ Swewsh.callback(direction);
}
+ Swewsh.cancel();
},
+
+ cancel: function() {
+ //Event listener garbage cleanup
+ document.removeEventListener('touchmove', Swewsh.move);
+ document.removeEventListener('touchend', Swewsh.end);
- init : function(event, callback) {
- Swewsh.callback = callback;
- document.addEventListener('touchstart', Swewsh.start, false);
+ //Reset variables for next Swewsh
+ Swewsh.x = null;
+ Swewsh.y = null;
+ Swewsh.dx = null;
}
};
-// This is the magic that gets fired after a Swewsh is registered.
+// This is the magic that gets fired after a Swewsh is registered.
function handleSwewsh(dir) {
alert(dir); //will either be "left" or "right", so an if/else or ternary operator will let you kick out your jams.
}
-//Specify callback function
+
+//Specify callback function, set this sucker to fire onload.
window.onload = function(event) {
Swewsh.init(event, handleSwewsh);
};
Please sign in to comment.
Something went wrong with that request. Please try again.