Skip to content

Conversation

@ernstnaezer
Copy link

Hi, I'm running the code on a touch screen and it's very difficult to keep your hand absolutely still to trigger the long click event. Removing the mousemove restriction fixes this issue. Is this something you might want to consider applying to the main branch?

… keep your hand absolutely still to trigger the long click event. Removing the mousemove restriction fixes this issue. Is this something you might want to consider applying to the main branch?
@TheSin-
Copy link

TheSin- commented Jul 6, 2015

I have this exact same issue.

But instead of removing mousemove could we not detect the amount of movement? Like set the original x,y of the mousedown event. then on mouse move check to see if the x,y are within tolerance before we cancel it completely?

I think a large mouse move should cancel it so this PR might not be the best long term solution.

@TheSin-
Copy link

TheSin- commented Jul 6, 2015

This is how I did it, this is a unified diff since I didn't have time to make a PR, but this checked movement size.

--- jquery.longclick.orig.js    2015-07-06 18:23:28.629104240 +0000
+++ jquery.longclick.js 2015-07-06 18:21:47.349731794 +0000
@@ -55,7 +55,9 @@
     * False value results in using the configured default.
     * Default `duration` is **500** and is stored in `jQuery.longclick.duration` variable.
     */
-    duration: 500
+    duration: 500,
+    origx: 0,
+    origy: 0
   }

   /*
@@ -67,7 +69,8 @@
         /* normal technique for standard mouse-based interaction */
         $(this)
         .bind(_mousedown_, schedule)
-        .bind([_mousemove_, _mouseup_, _mouseout_, _contextmenu_].join(' '), annul)
+        .bind(_mousemove_, moveck)
+        .bind([_mouseup_, _mouseout_, _contextmenu_].join(' '), annul)
         .bind(_click_, click)
       }else{
         /* and special handling for touch-based interaction on iPhone-compatibile devices */
@@ -103,6 +106,9 @@
     var
       element= this,
       args= arguments
+    /* Set original X,Y */
+    $.longclick.origx = event.offsetX
+    $.longclick.origy = event.offsetY
     /* Flag as "not fired" and schedule the trigger */
     $(this)
     .data(_fired_, false)
@@ -115,6 +121,13 @@
       jQuery.event.handle.apply(element, args)
     }
   }
+  function moveck(event){
+  /* check how much we move, touch devices make it hard to not move at all */
+    if (Math.abs($.longclick.origx - event.offsetX) > 10 ||
+        Math.abs($.longclick.origy - event.offsetY) > 10) {
+      annul(event)
+    }
+  }
   function annul(event){
     /* Annul the scheduled trigger */
     $(this).data(_timer_, clearTimeout($(this).data(_timer_)) || null)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants