-
Notifications
You must be signed in to change notification settings - Fork 2
/
swinch.min.js
12 lines (12 loc) · 1.53 KB
/
swinch.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
* swinch v0.1.0
*
* A lightweight, customisable, horizontal touch detection script
*
* Copyright (c) 2014 Matt Stow
*
* http://mattstow.com
*
* Licensed under the MIT license
*/
;window.swinch=function(a,b){a&&(this.options=b||{},this.thresholdDuration=this.options.thresholdDuration||50,this.thresholdDistance=this.options.thresholdDistance||30,this.onMove=this.options.onMove||function(){},this.onEnd=this.options.onEnd||function(){},a.addEventListener&&(a.addEventListener("touchstart",this,!1),a.addEventListener("touchmove",this,!1),a.addEventListener("touchend",this,!1),a.addEventListener("touchcancel",this,!1)))},swinch.prototype={handleEvent:function(a){switch(a.type){case"touchstart":this.onTouchStart(a);break;case"touchmove":this.onTouchMove(a);break;case"touchcancel":case"touchend":this.onTouchEnd(a)}},onTouchStart:function(a){this.start={pageX:a.touches[0].pageX,pageY:a.touches[0].pageY,time:Number(new Date)},this.isScrolling=void 0,this.deltaX=0,a.stopPropagation()},onTouchMove:function(a){a.touches.length>1||a.scale&&1!==a.scale||(this.deltaX=a.touches[0].pageX-this.start.pageX,"undefined"==typeof this.isScrolling&&(this.isScrolling=!!(this.isScrolling||Math.abs(this.deltaX)<Math.abs(a.touches[0].pageY-this.start.pageY))),this.isScrolling||(a.preventDefault(),a.stopPropagation(),this.onMove(this.deltaX)))},onTouchEnd:function(a){var b=Number(new Date)-this.start.time>this.thresholdDuration&&Math.abs(this.deltaX)>this.thresholdDistance;!this.isScrolling&&b&&this.onEnd(this.deltaX<0?"left":"right"),a.stopPropagation()}};