Skip to content

Commit

Permalink
Adds support for jQuery 1.8 as well as AMD loading.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephband committed Sep 6, 2012
1 parent 80f7bbd commit 46bdbe0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

<p>One of swipeleft, swiperight, swipeup or swipedown is triggered on moveend, when the move has covered at least a threshold proportion of the dimension of the target node.</p>

<p>Swipe events are a thin wrapper around the moveend event, a convenience to reveal when a finger has made a swipe gesture. As such they don't bubble &mdash; they are retriggered on each passing of a moveend event. The underlying move events do bubble and delegate. Use them if you need more flexibility.</p>

<h2>Demo and docs</h2>
<a href="http://stephband.info/jquery.event.swipe">stephband.info/jquery.event.swipe</a>
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ <h3>Who made this?</h3>
><div class="repo_col col">
<h3>More code</h3>
<ul class="repo_index index">
<li><a href="http://stephband.info/template">Template</a></li>
<li><a href="http://stephband.info/bolt">Bolt</a></li>
<li><a href="http://stephband.info/jparallax/">jQuery.parallax</a></li>
<li><a href="http://stephband.info/jquery.event.tap/">jQuery.event.tap</a></li>
<li><a href="http://stephband.info/jquery.event.move/">jQuery.event.move</a></li>
Expand Down
59 changes: 36 additions & 23 deletions js/jquery.event.swipe.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
// jQuery.event.swipe
// 0.4
// 0.5
// Stephen Band

// Dependencies
// jQuery.event.move
// jQuery.event.move 1.2

// One of swipeleft, swiperight, swipeup or swipedown is triggered on
// moveend, when the move has covered a threshold ratio of the dimension
// of the target node. The default is 0.4. It can be changed with:
//
// jQuery.event.special.swipe.settings

(function(jQuery, undefined){
// of the target node, or has gone really fast. Threshold and velocity
// sensitivity changed with:
//
// jQuery.event.special.swipe.settings.threshold
// jQuery.event.special.swipe.settings.sensitivity

(function (module) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], module);
} else {
// Browser globals
module(jQuery);
}
})(function(jQuery, undefined){
var add = jQuery.event.add,

remove = jQuery.event.remove,
Expand All @@ -22,15 +32,13 @@
jQuery.event.trigger(type, data, node);
},

// Ratio of the width (or height) of the target node must be
// swiped before being considered a swipe.
settings = {
// Ratio of distance over target finger must travel to be
// considered a swipe.
threshold: 0.4,
// Faster fingers can travel shorter distances to be considered
// swipes. 'sensitivity' controls how much. Bigger is shorter.
sensitivity: 2
sensitivity: 6
};

function moveend(e) {
Expand Down Expand Up @@ -79,14 +87,15 @@
}
}

function isSetup(node) {
var events = jQuery.data(node, 'events');

return ((events.swipe ? 1 : 0) +
(events.swipeleft ? 1 : 0) +
(events.swiperight ? 1 : 0) +
(events.swipeup ? 1 : 0) +
(events.swipedown ? 1 : 0)) > 1;
function getData(node) {
var data = jQuery.data(node, 'event_swipe');

if (!data) {
data = { count: 0 };
jQuery.data(node, 'event_swipe', data);
}

return data;
}

jQuery.event.special.swipe =
Expand All @@ -95,23 +104,27 @@
jQuery.event.special.swipeup =
jQuery.event.special.swipedown = {
setup: function( data, namespaces, eventHandle ) {
// If another swipe event is already setup, don't setup again.
if (isSetup(this)) { return; }
var data = getData(this);

// If another swipe event is already setup, don't setup again.
if (data.count++ > 0) { return; }
console.log('SETUP');
add(this, 'moveend', moveend);

return true;
},

teardown: function() {
// If another swipe event is still setup, don't teardown yet.
if (isSetup(this)) { return; }
var data = getData(this);

// If another swipe event is still setup, don't teardown.
if (--data.count > 0) { return; }
console.log('TEARDOWN');
remove(this, 'moveend', moveend);

return true;
},

settings: settings
};
})(jQuery);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jquery.event.swipe",
"title": "jquery.event.swipe",
"version": "0.4",
"version": "0.5",
"author": {
"name": "Stephen Band",
"url": "http://stephband.info",
Expand Down

0 comments on commit 46bdbe0

Please sign in to comment.