From 9118fa551c30ecda1b0e78af6418284460822d75 Mon Sep 17 00:00:00 2001 From: Eric Bidelman Date: Tue, 10 Apr 2012 22:47:35 -0700 Subject: polyfills working for ios < 5 --- js/touch.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 js/touch.js (limited to 'js/touch.js') diff --git a/js/touch.js b/js/touch.js new file mode 100644 index 0000000..e0519e6 --- /dev/null +++ b/js/touch.js @@ -0,0 +1,61 @@ +/* Touch events */ +(function(exports) { +var PM_TOUCH_SENSITIVITY = 15; +var touchDX; +var touchDY; +var touchStartX; +var touchStartY; + +function TouchManager(deck) { + this.deck_ = deck; + + /* Add swiping */ + document.body.addEventListener('touchstart', this.handleTouchStart.bind(this), false); + document.body.addEventListener('touchmove', this.handleTouchMove.bind(this), false); + document.body.addEventListener('touchend', this.handleTouchEnd.bind(this), false); +} + +TouchManager.prototype.handleTouchStart = function(e) { + if (e.touches.length == 1) { + touchDX = 0; + touchDY = 0; + + touchStartX = e.touches[0].pageX; + touchStartY = e.touches[0].pageY; + + } +}; + +TouchManager.prototype.handleTouchMove = function(e) { + if (e.touches.length > 1) { + //this.cancelTouch(); + } else { + touchDX = e.touches[0].pageX - touchStartX; + touchDY = e.touches[0].pageY - touchStartY; + } +}; + +TouchManager.prototype.handleTouchEnd = function(e) { + var dx = Math.abs(touchDX); + var dy = Math.abs(touchDY); + + if ((dx > PM_TOUCH_SENSITIVITY) && (dy < (dx * 2 / 3))) { + if (touchDX > 0) { + this.deck_.prevSlide(); + } else { + this.deck_.nextSlide(); + } + } + + //this.cancelTouch(); +}; + +// TouchManager.prototype.cancelTouch = function() { +// console.log(touchDX) +// document.body.removeEventListener('touchmove', this.handleTouchMove.bind(this), false); +// document.body.removeEventListener('touchend', this.handleTouchMove.bind(this), false); +// }; + +exports.TouchManager = TouchManager; + +})(window); -- cgit v1.2.3