Skip to content

Instantly share code, notes, and snippets.

@shabanovtg
Created December 16, 2015 06:48
Show Gist options
  • Save shabanovtg/4e79b06d2eb6aeef2765 to your computer and use it in GitHub Desktop.
Save shabanovtg/4e79b06d2eb6aeef2765 to your computer and use it in GitHub Desktop.

Revisions

  1. shabanovtg created this gist Dec 16, 2015.
    44 changes: 44 additions & 0 deletions swipe.simple.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    /**
    * Swipe
    */
    document.addEventListener('touchstart', handleTouchStart, false);
    document.addEventListener('touchmove', handleTouchMove, false);

    var xDown = null;
    var yDown = null;

    function handleTouchStart(ev) {
    console.log('handleTouchStart');
    xDown = ev.touches[0].clientX;
    yDown = ev.touches[0].clientY;
    }

    function handleTouchMove(ev) {
    console.log('handleTouchMove');
    if (!xDown || !yDown) {
    return;
    }

    var xUp = ev.touches[0].clientX;
    var yUp = ev.touches[0].clientY;

    var xDiff = xDown - xUp;
    var yDiff = yDown - yUp;

    if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) {/*most significant*/
    if ( xDiff > 0 ) {
    /* left swipe */
    } else {
    /* right swipe */
    }
    } else {
    if ( yDiff > 0 ) {
    /* up swipe */
    } else {
    /* down swipe */
    }
    }
    /* reset values */
    xDown = null;
    yDown = null;
    }