Repository: stephband/jquery.event.swipe Branch: master Commit: 355d015abf34 Files: 6 Total size: 18.0 KB Directory structure: gitextract_o4lzu_em/ ├── README.md ├── bower.json ├── event.swipe.jquery.json ├── index.html ├── js/ │ └── jquery.event.swipe.js └── package.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================

jquery.event.swipe

Dependencies

jQuery.event.move: stephband.info/jquery.event.move

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.

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 — 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.

CommonJS

If you're using Browserify, or any other CommonJS-compatible module system, you can require this script by passing it your jQuery reference. For example,


require('./path/to/jquery.event.move.js')(jQuery);

Demo and docs

stephband.info/jquery.event.swipe ================================================ FILE: bower.json ================================================ { "name": "jquery.event.swipe", "version": "0.5.3", "author": { "name": "Stephen Band", "url": "http://stephband.info", "twitter": "stephband" }, "dependencies": { "jquery": ">=1.7.0", "jquery.event.move": "1.x" }, "licenses": [{"url":"http://www.wtfpl.net/txt/copying/"}], "main": "js/jquery.event.swipe.js", "description": "jQuery special events for the gestures swipeleft, swiperight, swipeup and swipedown.", "keywords": ["touch","special","event","swipe"], "homepage": "http://github.com/stephband/jquery.event.swipe", "docs": "http://stephband.info/jquery.event.swipe" } ================================================ FILE: event.swipe.jquery.json ================================================ { "name": "event.swipe", "title": "jquery.event.swipe", "version": "0.5.3", "author": { "name": "Stephen Band", "url": "http://stephband.info" }, "dependencies": { "jquery": ">=1.7.0", "jquery.event.move": "1.3.x" }, "licenses": [{"url":"http://www.wtfpl.net/txt/copying/"}], "files": ["js/jquery.event.swipe.js"], "description": "jQuery special events for the gestures swipeleft, swiperight, swipeup and swipedown.", "keywords": ["touch","special","event","swipe"], "homepage": "http://github.com/stephband/jquery.event.swipe", "docs": "http://stephband.info/jquery.event.swipe" } ================================================ FILE: index.html ================================================ --- title: jQuery.event.swipe --- jQuery.event.swipe

jQuery.event.swipe

Project

Swipe events

swipeleft, swiperight, swipeup, swipedown
Fired when the finger is released from the touch surface (or mouse button) if the finger has a) just moved at least a threshold distance or b) moved quickly over a shorter distance. Also responds to mouse events.

Swipe event objects are augmented with the properties:

e.pageX, e.pageY
Current page coordinates of pointer.
e.startX, e.startY
Page coordinates the pointer had at the start of the swipe.
e.distX, e.distY
Distance finger moved during the swipe.
e.velocityX, e.velocityY
Velocity, in pixels/ms, averaged over the last few move events.

How to use swipe events

{% highlight js %} var slides = jQuery('.slides'), i = 0; slides .on('swipeleft', function(e) { slides.eq(i + 1).addClass('active'); }) .on('swiperight', function(e) { slides.eq(i - 1).addClass('active'); });{% endhighlight %}

Swipe events are a thin wrapper around the moveend event; a convenience to reveal when a finger has moved in a swipe gesture. The underlying move events bubble and you can delegate them, but making the swipe events also do this would be unnecessarily expensive. Use move events if you need more flexibility.

Swipe or scroll

Swipe events are a thin wrapper built on top of move events (stephband.info/jquery.event.move). Move events, by default, override native scrolling, as they assume that you want to move something rather than scroll the window. To re-enable scrolling, call e.preventDefault() inside a movestart handler.

In the example above, we want to be able to swipeleft and swiperight, but scroll up and down. Inside a movestart handler, the direction of the finger is moving is calculated and the event is prevented if it is found to be moving up or down::

{% highlight js %} jQuery('.mydiv') .on('movestart', function(e) { // If the movestart is heading off in an upwards or downwards // direction, prevent it so that the browser scrolls normally. if ((e.distX > e.distY && e.distX < -e.distY) || (e.distX < e.distY && e.distX > -e.distY)) { e.preventDefault(); } });{% endhighlight %}

To see how the example at the top of the page works, view source.

Who made it?

I did. I'm stephband on twitter, if you're into that sort of thing.

================================================ FILE: js/jquery.event.swipe.js ================================================ // jQuery.event.swipe // 0.5 // Stephen Band // Dependencies // 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, or has gone really fast. Threshold and velocity // sensitivity changed with: // // jQuery.event.special.swipe.settings.threshold // jQuery.event.special.swipe.settings.sensitivity (function (thisModule) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery', undefined, 'jquery.event.move'], thisModule); } else if ((typeof module !== "undefined" && module !== null) && module.exports) { module.exports = thisModule; } else { // Browser globals thisModule(jQuery); } })(function(jQuery, undefined){ var add = jQuery.event.add, remove = jQuery.event.remove, // Just sugar, so we can have arguments in the same order as // add and remove. trigger = function(node, type, data) { jQuery.event.trigger(type, data, node); }, 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: 6 }; function moveend(e) { var w, h, event; w = e.currentTarget.offsetWidth; h = e.currentTarget.offsetHeight; // Copy over some useful properties from the move event event = { distX: e.distX, distY: e.distY, velocityX: e.velocityX, velocityY: e.velocityY, finger: e.finger }; // Find out which of the four directions was swiped if (e.distX > e.distY) { if (e.distX > -e.distY) { if (e.distX/w > settings.threshold || e.velocityX * e.distX/w * settings.sensitivity > 1) { event.type = 'swiperight'; trigger(e.currentTarget, event); } } else { if (-e.distY/h > settings.threshold || e.velocityY * e.distY/w * settings.sensitivity > 1) { event.type = 'swipeup'; trigger(e.currentTarget, event); } } } else { if (e.distX > -e.distY) { if (e.distY/h > settings.threshold || e.velocityY * e.distY/w * settings.sensitivity > 1) { event.type = 'swipedown'; trigger(e.currentTarget, event); } } else { if (-e.distX/w > settings.threshold || e.velocityX * e.distX/w * settings.sensitivity > 1) { event.type = 'swipeleft'; trigger(e.currentTarget, event); } } } } 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 = jQuery.event.special.swipeleft = jQuery.event.special.swiperight = jQuery.event.special.swipeup = jQuery.event.special.swipedown = { setup: function( data, namespaces, eventHandle ) { var data = getData(this); // If another swipe event is already setup, don't setup again. if (data.count++ > 0) { return; } add(this, 'moveend', moveend); return true; }, teardown: function() { var data = getData(this); // If another swipe event is still setup, don't teardown. if (--data.count > 0) { return; } remove(this, 'moveend', moveend); return true; }, settings: settings }; }); ================================================ FILE: package.json ================================================ { "name": "jquery.event.swipe", "title": "jquery.event.swipe", "version": "0.5.4", "author": { "name": "Stephen Band", "url": "http://stephband.info", "twitter": "stephband" }, "dependencies": { "jquery": ">=1.7.0", "jquery.event.move": "1.x" }, "licenses": [{"url": "http://www.wtfpl.net/txt/copying/"}], "files": ["js/jquery.event.swipe.js"], "main": "js/jquery.event.swipe.js", "description": "jQuery special events for the gestures swipeleft, swiperight, swipeup and swipedown.", "keywords": ["touch","special","event","swipe","dom","jquery"], "homepage": "http://github.com/stephband/jquery.event.swipe", "docs": "http://stephband.info/jquery.event.swipe" }