dinoboff / jQswipe

jQuery swipe event

This URL has Read+Write access

dinoboff (author)
Mon Oct 26 04:39:43 -0700 2009
commit  67c67be917a6c6731584165cec32049c125ba23f
tree    9df933bde6812c111da65bbf4cb9c13f9f49f711
parent  7bcf1984fd4c797429a6e5eeef5eae4dcdd4afec
name age message
file .gitignore Sat Oct 24 04:42:03 -0700 2009 Add .gitignore to ignore dist/* [dinoboff]
file .gitmodules Sat Oct 24 04:25:35 -0700 2009 add demo submodule (for github pages) [dinoboff]
file LICENSE Loading commit data...
file Makefile Fri Oct 23 20:22:08 -0700 2009 tweaking for YUIcompressor [dinoboff]
file README.rst
directory build/ Fri Oct 23 20:06:21 -0700 2009 Add Makefile. [dinoboff]
submodule demo - 3774a65
file jqswipe.js
directory test/
file version.txt Fri Oct 23 20:06:21 -0700 2009 Add Makefile. [dinoboff]
README.rst

jQswipe

Add swipe (or rightSwipe) and leftSwipe events to jQuery. E.g, in the following example, swiping a task will reveal the link to remove it:

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
<script src="jQswipe.js" type="text/javascript"></script>

<ul id="tasks">
        <li>Task 1 <a href="#tasks">remove</a></li>
        <li>Task 2 <a href="#tasks">remove</a></li>
        <li>Task 3 <a href="#tasks">remove</a></li>
</ul>

<script type="text/javascript">
        // On click of 'complete' links, their list item are hidden,
        // but hide them for now.
        $('#tasks li > a').click(function(){
                $(this).parent().hide();
        }).hide();

        // On swipe, show the link.
        $('#tasks li').bind('swipe', function(){
                $(this).children('a').show();
        });
</script>

By default, jQswipe only add a swipe (or leftSwipe) and a rightSwipe event, but You can add your own swipe event by defining a validator to validate each finger position received (on a touchmove event) and its final position (on the touchend event). E.g., how jQswipe add the leftSwipe:

$.jQswipe.register('leftSwipe', function(el, newPoint) {
        var diffWithStart = points.current().diff(points.start()),
                diffWithPrevious = points.current().diff(points.previous());

        // Should not be hight or too low
        if (diffWithStart.y > this.threshold.maxWidth || diffWithStart.y < -this.threshold.maxWidth) {
                return false;
        }

        // should not move back to the right
        if (diffWithPrevious.x > 0) {
                return false;
        }

        // If swipe ended, the swipe be long enough
        if (this.ended(el) && diffWithStart.x > -this.threshold.minLength) {
                return false;
        }

        return true;
});

$.jQswipe.register, register an event type, e.g. rightSwipe, with a validator, in this case an anonymous function which takes two arguments:

  • el is the target element of the event,
  • points are the points recorded for this swipe.

this is a Swipe object.