Skip to content

Instantly share code, notes, and snippets.

@thomasjbradley
Last active September 1, 2020 05:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasjbradley/c0f75648bc9282297a97d92572dcd462 to your computer and use it in GitHub Desktop.
Save thomasjbradley/c0f75648bc9282297a97d92572dcd462 to your computer and use it in GitHub Desktop.
Swiper—a really basic left/right swiping event handler that tries to be helpful and not interfere with up/down scrolling.

Swiper

Swiper—a really basic left/right swiping event handler that tries to be helpful and not interfere with up/down scrolling.


Usage

Using Swiper is really simple: call the swiper function, passing an element and a callback.

When the element is swiped your callback will be passed the target and the direction.

var elem = document.body;

swiper(elem, function (e) {
  if (e.direction === 'left') {
    // Do something when swiped in the leftwards direction
  } else {
    // Do something when swiped in the rightwards direction
  }
});

API

  • swiper(elem, callback) — The only available function, will listen for horizontal swipes on the element and execute the callback.

    Callback signature: callback(e) — Only a single argument is passed, like event listeners, it contains two pieces of information:

    1. e.target — the thing that was swiped
    2. e.direction — the direction of the swipe: 'left' or 'right'

If you capture the response from the swiper() function you get access to a few other things:

var swipe = swiper(document.body, function (e) { });
  • .off() — Will disabled Swiper and stop listening. There’s also .noSwiping() for those cool people who want to write swiper.noSwiping().

  • .directions — An object that has the left and right entries in it if you don’t want to write the actual strings in your if statements. You could do something like this:

    var swipe = swiper(document.body, function (e) {
      if (e.direction === swipe.directions.left) console.log('Swiped left');
    });

License & copyright

© 2017 Thomas J Bradley
Licensed under the MIT License

MIT License
Copyright (c) 2017 Thomas J Bradley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
/*
* (c) 2017 Thomas J Bradley
* Licensed under the MIT License
*/
(function () {
'use strict';
var swiper = function (elem, callback) {
var directions = {left : 'left', right : 'right'};
var returnObject = {target : elem, direction : directions.left};
var originalCoord = {x : 0, y : 0};
var finalCoord = {x : 0, y : 0};
var changeX = 0;
var goingVertical = 0;
var callbackTimeout;
var touchStartHandler = function (e) {
originalCoord.x = e.targetTouches[0].pageX;
originalCoord.y = e.targetTouches[0].pageY;
finalCoord.x = originalCoord.x;
finalCoord.y = originalCoord.y;
goingVertical = 0;
clearTimeout(callbackTimeout);
};
var touchMoveHandler = function (e) {
finalCoord.x = e.targetTouches[0].pageX;
finalCoord.y = e.targetTouches[0].pageY;
if (goingVertical === 0) {
goingVertical = false;
if (Math.abs(finalCoord.y - originalCoord.y) > Math.abs(finalCoord.x - originalCoord.x)) {
goingVertical = true;
}
}
if (goingVertical === false) {
e.preventDefault();
}
};
var touchEndHandler = function () {
changeX = originalCoord.x - finalCoord.x;
clearTimeout(callbackTimeout);
if (goingVertical === false) {
if (changeX > 0) {
// Timeout stops the callback being fired if the tablet catches a scroll first
// Stops weird DOM freezing of iOS, they will then be triggered after scroll
returnObject.direction = directions.left;
callbackTimeout = setTimeout(function () { callback(returnObject); }, 10);
} else {
returnObject.direction = directions.right;
callbackTimeout = setTimeout(function () { callback(returnObject); }, 10);
}
}
goingVertical = 0;
};
var addSwipeListener = function () {
elem.addEventListener('touchstart', touchStartHandler, false);
elem.addEventListener('touchmove', touchMoveHandler, false);
elem.addEventListener('touchend', touchEndHandler, false);
};
var removeSwipeListener = function () {
elem.removeEventListener('touchstart', touchStartHandler);
elem.removeEventListener('touchmove', touchMoveHandler);
elem.removeEventListener('touchend', touchEndHandler);
};
addSwipeListener();
return {
off : removeSwipeListener,
noSwiping : removeSwipeListener,
directions : directions
};
};
window.swiper = swiper;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment