From 3b1c37440e3d1777e53df792f8f5c729cfeb2eea Mon Sep 17 00:00:00 2001 From: Hyunje Alex Jun Date: Fri, 14 Jun 2013 21:57:24 +0900 Subject: [PATCH] Inhance the scroll logic with mobile touches. The scroll works only when the user touch and scroll the wrapper div with just one touch. By this patch, the zoom function will not be prevented by scrolling content. --- src/perfect-scrollbar.js | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/perfect-scrollbar.js b/src/perfect-scrollbar.js index bcd3efe..2121905 100644 --- a/src/perfect-scrollbar.js +++ b/src/perfect-scrollbar.js @@ -236,7 +236,15 @@ var startCoords = {}, startTime = 0, speed = {}, - breakingProcess = null; + breakingProcess = null, + inGlobalTouch = false; + + $(window).bind("touchstart.perfect-scroll", function (e) { + inGlobalTouch = true; + }); + $(window).bind("touchend.perfect-scroll", function (e) { + inGlobalTouch = false; + }); $this.bind("touchstart.perfect-scroll", function (e) { var touch = e.originalEvent.targetTouches[0]; @@ -249,26 +257,30 @@ if (breakingProcess !== null) { clearInterval(breakingProcess); } + + e.stopPropagation(); }); $this.bind("touchmove.perfect-scroll", function (e) { - var touch = e.originalEvent.targetTouches[0]; + if (!inGlobalTouch && e.originalEvent.targetTouches.length === 1) { + var touch = e.originalEvent.targetTouches[0]; - var currentCoords = {}; - currentCoords.pageX = touch.pageX; - currentCoords.pageY = touch.pageY; + var currentCoords = {}; + currentCoords.pageX = touch.pageX; + currentCoords.pageY = touch.pageY; - var differenceX = currentCoords.pageX - startCoords.pageX, - differenceY = currentCoords.pageY - startCoords.pageY; + var differenceX = currentCoords.pageX - startCoords.pageX, + differenceY = currentCoords.pageY - startCoords.pageY; - applyTouchMove(differenceX, differenceY); - startCoords = currentCoords; + applyTouchMove(differenceX, differenceY); + startCoords = currentCoords; - var currentTime = (new Date()).getTime(); - speed.x = differenceX / (currentTime - startTime); - speed.y = differenceY / (currentTime - startTime); - startTime = currentTime; + var currentTime = (new Date()).getTime(); + speed.x = differenceX / (currentTime - startTime); + speed.y = differenceY / (currentTime - startTime); + startTime = currentTime; - e.preventDefault(); + e.preventDefault(); + } }); $this.bind("touchend.perfect-scroll", function (e) { breakingProcess = setInterval(function () {