Skip to content

Commit

Permalink
Resolve touch through issue with a global timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrybendy committed Dec 24, 2020
1 parent c0c3e4a commit 71449a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ var isPassiveSupported = (function() {
return supportsPassive;
})();

// Save last touch time globally (touch start time or touch end time), if a `click` event triggered,
// and the time near by the last touch time, this `click` event will be ignored. This is used for
// resolve touch through issue.
var globalLastTouchTime = 0;

var vueTouchEvents = {
install: function (Vue, constructorOptions) {
Expand All @@ -51,10 +55,10 @@ var vueTouchEvents = {
$el = this;

if (isTouchEvent) {
$this.lastTouchStartTime = event.timeStamp;
globalLastTouchTime = event.timeStamp;
}

if (isMouseEvent && $this.lastTouchStartTime && event.timeStamp - $this.lastTouchStartTime < 350) {
if (isMouseEvent && globalLastTouchTime && event.timeStamp - globalLastTouchTime < 350) {
return;
}

Expand Down Expand Up @@ -131,7 +135,7 @@ var vueTouchEvents = {
isMouseEvent = event.type.indexOf('mouse') >= 0;

if (isTouchEvent) {
$this.lastTouchEndTime = event.timeStamp;
globalLastTouchTime = event.timeStamp;
}

var touchholdEnd = isTouchEvent && !$this.touchHoldTimer;
Expand All @@ -141,7 +145,7 @@ var vueTouchEvents = {

removeTouchClass(this);

if (isMouseEvent && $this.lastTouchEndTime && event.timeStamp - $this.lastTouchEndTime < 350) {
if (isMouseEvent && globalLastTouchTime && event.timeStamp - globalLastTouchTime < 350) {
return;
}

Expand Down Expand Up @@ -298,6 +302,7 @@ var vueTouchEvents = {
// change the passive option for the moving event if disablePassive modifier exists
passiveOpt = false;
}
// fallthrough
default:
$this.callbacks[eventType] = $this.callbacks[eventType] || [];
$this.callbacks[eventType].push(binding);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue2-touch-events",
"version": "3.0.1",
"version": "3.1.0",
"description": "Simple touch events support for vueJS2",
"main": "index.js",
"types": "index.d.ts",
Expand Down

0 comments on commit 71449a9

Please sign in to comment.