Skip to content

Instantly share code, notes, and snippets.

@SleepWalker
Created September 30, 2015 04:59
Show Gist options
  • Save SleepWalker/da5636b1abcbaff48c4d to your computer and use it in GitHub Desktop.
Save SleepWalker/da5636b1abcbaff48c4d to your computer and use it in GitHub Desktop.

Revisions

  1. SleepWalker created this gist Sep 30, 2015.
    36 changes: 36 additions & 0 deletions swipe.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    var touchstartX = 0;
    var touchstartY = 0;
    var touchendX = 0;
    var touchendY = 0;

    var gesuredZone = document.getElementById('gesuredZone');

    gesuredZone.addEventListener('touchstart', function(event) {
    touchstartX = event.screenX;
    touchstartY = event.screenY;
    }, false);

    gesuredZone.addEventListener('touchend', function(event) {
    touchendX = event.screenX;
    touchendY = event.screenY;
    handleGesure();
    }, false);

    function handleGesure() {
    var swiped = 'swiped: ';
    if (touchendX < touchstartX) {
    alert(swiped + 'left!');
    }
    if (touchendX > touchstartX) {
    alert(swiped + 'right!');
    }
    if (touchendY < touchstartY) {
    alert(swiped + 'down!');
    }
    if (touchendY > touchstartY) {
    alert(swiped + 'left!');
    }
    if (touchendY == touchstartY) {
    alert('tap!');
    }
    }