From ba255ec4540a19f7127adfa2c18b8bc2974b4b13 Mon Sep 17 00:00:00 2001 From: zhangdonghua <1141372981@qq.com> Date: Thu, 9 Nov 2017 11:19:39 +0800 Subject: [PATCH] use modernizr Touch Events detect to handle Mobile shortpressHandler trigger twice bug --- jquery.longpress.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/jquery.longpress.js b/jquery.longpress.js index b061ece..50ad0d0 100644 --- a/jquery.longpress.js +++ b/jquery.longpress.js @@ -15,6 +15,12 @@ */ (function($) { + // use modernizr Touch Events detect + // to handle Mobile shortpressHandler trigger twice bug + var bool; + if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) { + bool = true; + } $.fn.longpress = function(longCallback, shortCallback, duration) { if (typeof duration === "undefined") { duration = 500; @@ -65,15 +71,18 @@ clearTimeout(timeout); } + if(bool){ + // Mobile Support + $this.on('touchstart', mousedown_callback); + $this.on('touchend', mouseup_callback); + $this.on('touchmove', move_callback); + return; + } // Browser Support $this.on('mousedown', mousedown_callback); $this.on('mouseup', mouseup_callback); $this.on('mousemove', move_callback); - // Mobile Support - $this.on('touchstart', mousedown_callback); - $this.on('touchend', mouseup_callback); - $this.on('touchmove', move_callback); }); }; }(jQuery));