Created
April 21, 2016 11:30
-
-
Save culttm/42658c3e26d4a040d29efefb7c2d98dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var DetectSwipeDirection; | |
| DetectSwipeDirection = (function(){ | |
| function DetectSwipeDirection(options){ | |
| var defaults = { | |
| delta: 0, | |
| leftCallback: function(){}, | |
| rightCallback: function(){}, | |
| upCallback: function(){}, | |
| downCallback: function(){} | |
| }; | |
| this.xDown = null; | |
| this.yDown = null; | |
| this.options = $.extend(defaults, options); | |
| this.init(); | |
| } | |
| DetectSwipeDirection.prototype.init = function(){ | |
| this._registerListeners(); | |
| }; | |
| DetectSwipeDirection.prototype._registerListeners = function(){ | |
| var self = this; | |
| document.addEventListener('touchstart', function(e){ | |
| self._handleTouchStart(e); | |
| }, false); | |
| document.addEventListener('touchmove', function(e){ | |
| self._handleTouchMove(e); | |
| }, false); | |
| }; | |
| DetectSwipeDirection.prototype._handleTouchStart = function(e){ | |
| this.xDown = e.touches[0].clientX; | |
| this.yDown = e.touches[0].clientY; | |
| }; | |
| DetectSwipeDirection.prototype.getDirection = function(e){ | |
| var options = this.options; | |
| var delta = parseInt(options.delta) || 0; | |
| if ( !e || !this.xDown || !this.yDown ) { | |
| return; | |
| } | |
| var xUp = e.touches[0].clientX; | |
| var yUp = e.touches[0].clientY; | |
| var xDiff = this.xDown - xUp; | |
| var yDiff = this.yDown - yUp; | |
| if ( Math.abs( xDiff ) > Math.abs( yDiff ) ) { | |
| if ( xDiff > delta ) { | |
| return 'left'; | |
| }else{ | |
| return 'right'; | |
| } | |
| } else { | |
| if ( yDiff > delta ) { | |
| return 'up'; | |
| }else{ | |
| return 'down'; | |
| } | |
| } | |
| }; | |
| DetectSwipeDirection.prototype._handleTouchMove = function(e) { | |
| var options = this.options, | |
| direction = this.getDirection(e); | |
| switch (direction){ | |
| case 'left':{ | |
| if(options.leftCallback && typeof options.leftCallback === 'function'){ | |
| options.leftCallback.call(null, this) | |
| } | |
| break | |
| } | |
| case 'right':{ | |
| if(options.rightCallback && typeof options.rightCallback === 'function'){ | |
| options.rightCallback.call(null, this) | |
| } | |
| break | |
| } | |
| case 'up':{ | |
| if(options.upCallback && typeof options.upCallback === 'function'){ | |
| options.upCallback.call(null, this) | |
| } | |
| break | |
| } | |
| case 'down':{ | |
| if(options.downCallback && typeof options.downCallback === 'function'){ | |
| options.downCallback.call(null, this) | |
| } | |
| break | |
| } | |
| default: return; | |
| } | |
| }; | |
| return DetectSwipeDirection; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment