We provide a set of "virtual" mouse events that attempt to abstract away mouse and touch events. This allows the developer to register listeners for the basic mouse events, such as mousedown, mousemove, mouseup, and click, and the plugin will take care of registering the correct listeners behind the scenes to invoke the listener at the fastest possible time for that device. In touch environments, the plugin retains the order of event firing that is seen in traditional mouse environments, so for example, vmouseup is always dispatched before vmousedown, and vmousedown before vclick, etc. The virtual mouse events also normalize how coordinate information is extracted from the event, so in touch based environments, coordinates are available from the pageX, pageY, screenX, screenY, clientX, and clientY properties, directly on the event object.
The jQuery Mobile "vclick"
event handler simulates the "onclick" event handler on mobile devices.
This plugin extends jQuery's built-in method. If jQuery Mobile is not loaded, calling the .vclick()
method may not fail directly, as the method still exists. However, the expected behavior will not occur.
$( document ).on( "vclick", "p", function() { $( this ).append( "<span style='color:#108040;'> vclick fired... </span>" ); });
Warning: Use vclick with caution
Use vclick with caution on touch devices. Webkit based browsers synthesize mousedown
, mouseup
, and click
events roughly 300ms after the touchend
event is dispatched. The target of the synthesized mouse events are calculated at the time they are dispatched and are based on the location of the touch events and, in some cases, the implementation specific heuristics which leads to different target calculations on different devices and even different OS versions for the same device. This means the target element within the original touch events could be different from the target element within the synthesized mouse events.
We recommend using click
instead of vclick
anytime the action being triggered has the possibility of changing the content underneath the point that was touched on screen. This includes page transitions and other behaviors such as collapse/expand that could result in the screen shifting or content being completely replaced.
Canceling an element's default click behavior
Applications can call preventDefault()
on a vclick
event to cancel an element's default click behavior. On mouse based devices, calling preventDefault()
on a vclick
event equates to calling preventDefault()
on the real click
event during the bubble event phase. On touch based devices, it's a bit more complicated since the actual click
event is dispatched about 300ms after the vclick
event is dispatched. For touch devices, calling preventDefault()
on a vclick
event triggers some code in the vmouse plugin that attempts to catch the next click
event that gets dispatched by the browser, during the capture event phase, and calls preventDefault()
and stopPropagation()
on it. As mentioned in the warning above, it is sometimes difficult to match up a touch event with its corresponding mouse event because the targets can differ. For this reason, the vmouse plugin also falls back to attempting to identify a corresponding click
event by coordinates. There are still cases where both target and coordinate identification fail, which results in the click
event being dispatched and either triggering the default action of the element, or in the case where content has been shifted or replaced, triggering a click on a different element. If this happens on a regular basis for a given element/control, we suggest you use click
for triggering your action.
The virtual mouse events can also be configured:
-
$.vmouse.moveDistanceThreshold
(default: 10px) â More than this, then it is a scroll event. The vmousecancel event is called and the TouchMove event is cancelled. -
$.vmouse.clickDistanceThreshold
(default: 10px) â If a vclick event was already captured and is in the block list, then vclicks less than this distance are ignored. -
$.vmouse.resetTimerDuration
(default: 1500ms) â More time than this, then it is not a touch event. Scroll, TouchMove and TouchEnd events use this. The block list is cleared.
Please login to continue.