From 7dd633596503a691d56e58826d405f771c5f5c99 Mon Sep 17 00:00:00 2001 From: Hyunje Alex Jun Date: Fri, 24 Oct 2014 11:26:54 +0100 Subject: [PATCH] Filter touch events not to be handled. In IE 10/11, MSPointer event is fired by mouse. It shouldn't be handled. Also, Exception has occurred with event.targetTouches, and this commit fixed it too. --- src/perfect-scrollbar.js | 65 ++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 23 deletions(-) diff --git a/src/perfect-scrollbar.js b/src/perfect-scrollbar.js index ffef5c0..20600ec 100644 --- a/src/perfect-scrollbar.js +++ b/src/perfect-scrollbar.js @@ -544,6 +544,7 @@ var speed = {}; var breakingProcess = null; var inGlobalTouch = false; + var inLocalTouch = false; function globalTouchStart(e) { inGlobalTouch = true; @@ -560,23 +561,37 @@ return e.originalEvent; } } - function touchStart(e) { - var touch = getTouch(e); - - startOffset.pageX = touch.pageX; - startOffset.pageY = touch.pageY; - - startTime = (new Date()).getTime(); - - if (breakingProcess !== null) { - clearInterval(breakingProcess); + function shouldHandle(e) { + var event = e.originalEvent; + if (event.targetTouches && event.targetTouches.length === 1) { + return true; } + if (event.pointerType && event.pointerType !== 'mouse') { + return true; + } + return false; + } + function touchStart(e) { + if (shouldHandle(e)) { + inLocalTouch = true; - e.stopPropagation(); - e.preventDefault(); + var touch = getTouch(e); + + startOffset.pageX = touch.pageX; + startOffset.pageY = touch.pageY; + + startTime = (new Date()).getTime(); + + if (breakingProcess !== null) { + clearInterval(breakingProcess); + } + + e.stopPropagation(); + e.preventDefault(); + } } function touchMove(e) { - if (!inGlobalTouch && e.originalEvent.targetTouches.length === 1) { + if (!inGlobalTouch && inLocalTouch && shouldHandle(e)) { var touch = getTouch(e); var currentOffset = {pageX: touch.pageX, pageY: touch.pageY}; @@ -601,18 +616,22 @@ } } function touchEnd(e) { - clearInterval(breakingProcess); - breakingProcess = setInterval(function () { - if (Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) { - clearInterval(breakingProcess); - return; - } + if (!inGlobalTouch && inLocalTouch) { + inLocalTouch = false; - applyTouchMove(speed.x * 30, speed.y * 30); + clearInterval(breakingProcess); + breakingProcess = setInterval(function () { + if (Math.abs(speed.x) < 0.01 && Math.abs(speed.y) < 0.01) { + clearInterval(breakingProcess); + return; + } - speed.x *= 0.8; - speed.y *= 0.8; - }, 10); + applyTouchMove(speed.x * 30, speed.y * 30); + + speed.x *= 0.8; + speed.y *= 0.8; + }, 10); + } } if (supportsTouch) {