From d9a0ad2cdb57f1a3d4eda657f35b2cea079e3cf8 Mon Sep 17 00:00:00 2001 From: Simon Ratner Date: Fri, 23 Mar 2012 16:52:46 -0700 Subject: [PATCH 1/2] Don't flip direction to zero when mouse/touch is stationary. Mousemove/touchmove events keep on firing (once a second or so) even if the position is not changing. This resulted in the direction being reset to zero if one holds the mouse/finger stationary for a while, resulting in incorrect behaviour once the mouse/finger is released. --- src/swipeview.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/swipeview.js b/src/swipeview.js index 2546ee0..0dbe310 100644 --- a/src/swipeview.js +++ b/src/swipeview.js @@ -275,7 +275,9 @@ var SwipeView = (function(){ this.moved = true; this.pointX = point.pageX; this.pointY = point.pageY; - this.directionX = deltaX > 0 ? 1 : deltaX < 0 ? -1 : 0; + if (deltaX !== 0) { + this.directionX = deltaX > 0 ? 1 : -1; + } this.stepsX += Math.abs(deltaX); this.stepsY += Math.abs(deltaY); From f1e1904746e31111d19af98c20ca623a62d12a25 Mon Sep 17 00:00:00 2001 From: Simon Ratner Date: Tue, 27 Mar 2012 16:56:11 -0700 Subject: [PATCH 2/2] fix off-by-one during partial swipes --- src/swipeview.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/swipeview.js b/src/swipeview.js index 0dbe310..798ade8 100644 --- a/src/swipeview.js +++ b/src/swipeview.js @@ -320,7 +320,7 @@ var SwipeView = (function(){ if (!this.initiated) return; var point = hasTouch ? e.changedTouches[0] : e, - dist = Math.abs(point.pageX - this.startX); + dist = point.pageX - this.startX; this.initiated = false; @@ -332,8 +332,14 @@ var SwipeView = (function(){ } // Check if we exceeded the snap threshold - if (dist < this.snapThreshold) { - this.slider.style.webkitTransitionDuration = Math.floor(300 * dist / this.snapThreshold) + 'ms'; + if (Math.abs(dist) < this.snapThreshold) { + this.slider.style.webkitTransitionDuration = Math.floor(300 * Math.abs(dist) / this.snapThreshold) + 'ms'; + this.__pos(-this.page * this.pageWidth); + return; + } + // Check if swipe was cancelled by reversing swipe direction + if ((dist < 0 && this.directionX >= 0) || (dist > 0 && this.directionX <= 0)) { + this.slider.style.webkitTransitionDuration = Math.floor(300 * Math.abs(dist) / this.pageWidth) + 'ms'; this.__pos(-this.page * this.pageWidth); return; }