Skip to content

Latest commit

 

History

History
100 lines (63 loc) · 4.9 KB

README.md

File metadata and controls

100 lines (63 loc) · 4.9 KB

FastClick

FastClick is a simple, easy-to-use library for eliminating the 300ms delay between a physical tap and the firing of a click event on mobile browsers. The aim is to make your application feel less laggy and more responsive while avoiding any interference with your current logic.

FastClick is developed by FT Labs, part of the Financial Times.

Explication en français.

日本語で説明

Краткое пояснение на русском языке.

Compatibility

The library has been deployed as part of the FT Web App and is tried and tested on the following mobile browsers:

  • Mobile Safari on iOS 3 and upwards
  • Chrome on iOS 5 and upwards
  • Chrome on Android (ICS)
  • Opera Mobile 11.5 and upwards
  • Android Browser since Android 2
  • PlayBook OS 1 and upwards

FastClick doesn't attach any listeners on desktop browsers as it is not needed. Those that have been tested are:

  • Safari
  • Chrome
  • Internet Explorer
  • Firefox
  • Opera

Usage

Include fastclick.js in your JavaScript bundle or add it to your HTML page like this:

<script type='application/javascript' src='/path/to/fastclick.js'></script>

The script must be loaded prior to instantiating FastClick on any element of the page.

To instantiate FastClick on the body, which is the recommended method of use:

window.addEventListener('load', function() {
	new FastClick(document.body);
}, false);

Google Closure Compiler

FastClick supports compilation with ADVANCED_OPTIMIZATIONS ('advanced mode'), which should reduce its size by about 70% (60% gzipped). Note that exposure of the FastClick variable isn't forced therefore you must compile it along with all of your code.

AMD

FastClick has AMD (Asynchronous Module Definition) support. This allows it to be lazy-loaded with an AMD loader, such as RequireJS.

Component

FastClick comes with support for installation via the Component package manager.

NPM

Installation via the Node Package Manager is supported, although Component is preferred as this is not strictly a Node package.

Minification

Run bin/minify to build a minified version of FastClick using the Closure Compiler REST API. The minified file is saved to build/fastclick.min.js.

Advanced

Ignore certain elements with needsclick class

Sometimes you need FastClick to ignore certain elements. You can do this easily by adding the needsclick class.

<a class="needsclick">Ignored by FastClick</a>

Use case 1: non-synthetic click required

Internally, FastClick uses document.createEvent to fire a synthetic click event as soon as touchend is fired by the browser. It then suppresses the additional click event created by the browser after that. In some cases, the non-synthetic click event created by the browser is required, as described in the triggering focus example.

This is where the needsclick class comes in. Add the class to any element that requires a non-synthetic click.

Use case 2: Twitter Bootstrap 2.2.2 dropdowns

Another example of when to use the needsclick class is with dropdowns in Twitter Bootstrap 2.2.2. Bootstrap add its own touchstart listener for dropdowns, so you want to tell FastClick to ignore those. If you don't, touch devices will automatically close the dropdown as soon as it is clicked, because both FastClick and Bootstrap execute the synthetic click, one opens the dropdown, the second closes it immediately after.

<a class="dropdown-toggle needsclick" data-toggle="dropdown">Dropdown</a>

Examples

FastClick is designed to cope with many different browser oddities. Here are some examples to illustrate this:

Credits and collaboration

The lead developer of FastClick is Rowan Beentje at FT Labs. This fork is currently maintained by Matthew Caruana Galizia. All open source code released by FT Labs is licenced under the MIT licence. We welcome comments, feedback and suggestions. Please feel free to raise an issue or pull request. Enjoy.