# photonic/3.37/include/ext/jquery.detect_swipe.js

Photonic Gallery &amp; Lightbox for Flickr, SmugMug &amp; Others, version 3.37. 84 lines.

- Page: https://pluginprobe.com/plugins/photonic/3.37/code/include/ext/jquery.detect_swipe.js
- Raw: https://pluginprobe.com/plugins/photonic/3.37/raw/include/ext/jquery.detect_swipe.js
- Modified: 2020-12-14T22:09:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/photonic/3.37/code/include/ext/jquery.detect_swipe.js#L10-L20`.

```javascript
/**
 * jquery.detectSwipe v2.1.3
 * jQuery Plugin to obtain touch gestures from iPhone, iPod Touch, iPad and Android
 * http://github.com/marcandre/detect_swipe
 * Based on touchwipe by Andreas Waltl, netCU Internetagentur (http://www.netcu.de)
 */

(function (factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD. Register as an anonymous module.
		define(['jquery'], factory);
	} else if (typeof exports === 'object') {
		// Node/CommonJS
		module.exports = factory(require('jquery'));
	} else {
		// Browser globals
		factory(jQuery);
	}
}(function($) {

	$.detectSwipe = {
		version: '2.1.2',
		enabled: 'ontouchstart' in document.documentElement,
		preventDefault: true,
		threshold: 20
	};

	var startX,
		startY,
		isMoving = false;

	function onTouchEnd() {
		this.removeEventListener('touchmove', onTouchMove);
		this.removeEventListener('touchend', onTouchEnd);
		isMoving = false;
	}

	function onTouchMove(e) {
		if ($.detectSwipe.preventDefault) { e.preventDefault(); }
		if(isMoving) {
			var x = e.touches[0].pageX;
			var y = e.touches[0].pageY;
			var dx = startX - x;
			var dy = startY - y;
			var dir;
			if(Math.abs(dx) >= $.detectSwipe.threshold) {
				dir = dx > 0 ? 'left' : 'right'
			}
			else if(Math.abs(dy) >= $.detectSwipe.threshold) {
				dir = dy > 0 ? 'up' : 'down'
			}
			if(dir) {
				onTouchEnd.call(this);
				$(this).trigger('swipe', dir).trigger('swipe' + dir);
			}
		}
	}

	function onTouchStart(e) {
		if (e.touches.length == 1) {
			startX = e.touches[0].pageX;
			startY = e.touches[0].pageY;
			isMoving = true;
			this.addEventListener('touchmove', onTouchMove, false);
			this.addEventListener('touchend', onTouchEnd, false);
		}
	}

	function setup() {
		this.addEventListener && this.addEventListener('touchstart', onTouchStart, false);
	}

	function teardown() {
		this.removeEventListener('touchstart', onTouchStart);
	}

	$.event.special.swipe = { setup: setup };

	$.each(['left', 'up', 'down', 'right'], function () {
		$.event.special['swipe' + this] = { setup: function(){
			$(this).on('swipe', $.noop);
		} };
	});
}));
```
