This is a series of plugins that create additional events that can be used in combination with jQuery when developing for mobile devices. The events are also compatible with desktop browsers to ensure ultimate compatibility for your projects. In time, we will update the Repository to include some very basic demos to get you started with using the events, but for now, we've put together a list of the events that are provided, and what they do.
As explained, the events are each triggered by native touch events, or alternatively by click events. The plugin automatically detects whether the user's device is touch compatible, and will use the correct native events whenever required. It is hoped that these events will help to aid single-environment development with jQuery for mobile web app development.
All of the events outlined above have been written using jQuery's event.special object, and so can be used in conjuction with jQuery's event handling functions, as well as shortcut wrappers.
Binding a tap event to an element:
$('#myElement').bind('tap', function(e) { console.log('User tapped #myElement'); });
Using with .on() and .live():
$('#myElement').live('tap', function(e) { console.log('User tapped #myElement'); });
$('#myElement').on('tap', function(e) { console.log('User tapped #myElement'); });
Triggering the event:
$('#myElement').trigger('tap');
Removing the event with .off(), .die() and .unbind():
$('#myElement').off('tap', hander);
$('#myElement').die('tap', hander);
$('#myElement').unbind('tap', hander);
Using method wrapper:
$('#myElement').tap(function(e) { console.log('User tapped #myElement'); });
Method chaining. Chaining has also been preserved, so you can easily use these events in conjuction with other jQuery functions, or attach multiple events in a single, chained LOC:
$('#myElement').singletap(function() { console.log('singletap'); }).doubletap(function() { console.log('doubletap'); });
You can also define custom thresholds to be used for swipe events (swipeup, swiperight, swipedown and swipeleft) to prevent interference with scrolling and other events. To do so, simply assign a data-xthreshold or date-ythreshold to the target element as follows:
The value you define is the difference in pixels that the user must move before the event is triggered on the target element. If no threshold is defined, a default of 50px will be used.
data-xthreshold
defines the horizontal threshold.
data-ythreshold
defines the vertical threshold.