• Stars
    star
    426
  • Rank 101,884 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✌️ jQuery touch & gestures, fingers in the nose.

jQuery Finger Size Build Status Dependency Status

Notice

⚠️Not maintained anymore ⚠️

If you only seek to eliminate the 300ms tap delay, please first consider using native solutions as nearly all modern browsers now support one of them.


jQuery tap & gestures, fingers in the nose.

Finger unifies click and touch events by removing the 300ms delay on touch devices. It also provides a common set of events to handle basic gestures such as flick, drag, press and double tap.
Very small (< 0.5kb gzipped), it is focused on performance and KISS, is well tested and also supports jQuery delegated events.

Getting Started

Download the production version or the development version.
You can also install it via Jam or Bower.

In your web page:

<script src="jquery.js"></script>
<script src="dist/jquery.finger.min.js"></script>
<script>
  // direct event
  $('.touchme').on('tap', function() {
    console.log('direct');
  });

  // delegated event
  $('body').on('tap', '.touchme', function() {
    console.log('delegated');
  });
});
</script>

Documentation

Gestures

Finger focuses on one finger events:

      | tap | doubletap | press | drag | flick |

----------|-----|-----------|-------|------|-------| Available | βœ” | βœ” | βœ” | βœ” | βœ” |

Thresholds

You can tweak how Finger handles events by modifying thresholds found in the $.Finger object.

pressDuration

This is the time the user will have to hold in order to fire a press event. If this time is not reached, a tap event will be fired instead. This defaults to 300ms.

doubleTapInterval

This is the maximum time between two tap events to fire a doubletap event. If this time is reached, two distinct tap events will be fired instead. This defaults to 300ms.

flickDuration

This is the maximum time the user will have to swipe in order to fire a flick event. If this time is reached, only drag events will continue to be fired. This defaults to 150ms.

motionThreshold

This is the number of pixel the user will have to move in order to fire motion events (drag or flick). If this time is not reached, no motion will be handled and tap, doubletap or press event will be fired. This defaults to 5px.

Additional event parameters

Finger enhances the default event object when there is motion (drag & flick). It gives information about the pointer position and motion:

  • x: the x page coordinate.
  • y: the y page coordinate.
  • dx: this x delta (amount of pixels moved) since the last event.
  • dy: this y delta since the last event.
  • adx: this x absolute delta since the last event.
  • ady: this y absolute delta since the last event.
  • orientation:
    • horizontal: motion was detected as an horizontal one. This can be tweaked with $.Finger.motionThreshold.
    • vertical: motion was detected as a vertical one. This can be tweaked with $.Finger.motionThreshold.
  • direction:
    • 1: motion has a positive direction, either left to right for horizontal, or top to bottom for vertical.
    • -1: motion has a negative direction, either right to left for horizontal, or bottom to top for vertical.
  • end: true if motion has stopped, else false.

Prevent default behavior

There are three ways of preventing default behavior.

Globally

You can prevent every native behavior globally:

$.Finger.preventDefault = true;

Specifically

You can prevent default behavior, just like any other standard events:

$('body').on('tap', '.touchme', function(e) {
	// ...
	e.preventDefault();
});

Note that if you bind multiple events of the same type on the same element, and one of them is preventing default, every trigger of that event will implicitly prevent default of other bound events.

press event can only be prevented globally, not specifically.

Original event

Internally Finger binds a global touch / mouse event to do its duty. This native event can be accessed via the e.originalEvent property. This is a shared event, that means that this will be the same object across all your handlers.

With this original event you are able to decide how you want to prevent default behavior by adding any logic in any of your handlers.

This is an example on how to prevent horizontal scrolling, but not vertical:

$('body').on('drag', '.drag', function(e) {
	// let the default vertical scrolling happen
	if ('vertical' == e.orientation) return;

	// prevent default horizontal scrolling
	e.preventDefault();
});

Notes

This is how Finger prevents default behavior:

                    | tap | doubletap | press | drag | flick | globally |

------------------------|-----|-----------|-------|------|-------|----------| touchstart / mousedown | | | | | | βœ” | touchmove / mousemove | | | | βœ” | βœ” | | touchend / mouseup | βœ” | βœ” | | βœ” | βœ” | |

More details.

Examples

Remove the 300ms delay on every links of your page

$('body').on('tap', 'a', function(e) {
	window.location = $(this).attr('href');
	e.preventDefault();
});

Delegated events for dynamically loaded elements (AJAX):

$('body').on('tap', '.toggle', function() {
	$(this).toggleClass('is-selected');
});

Swipe to reveal

$('#menu').on('flick', function(e) {
	if ('horizontal' == e.orientation) {
		if (1 == e.direction) {
			$(this).addClass('is-opened');
		}
		else {
			$(this).removeClass('is-opened');
		}
	}
});

Notes

  • Finger uses VirtualPointer in its test suite to simulate mouse and touch events.
  • On Chrome 25+, preventDefault does not work as expected because ontouchstart is defined. To make it work, you have to manually prevent the default behavior in the mousedown or click event.
  • When using flick or drag event on an image, you have to set user-drag: none on it (and the prefixed versions).

Instacode

Projects using Finger

Release History

v0.1.6
 - improve press event handling (#49).

v0.1.5
 - ignore non-left mouse clicks (#48).

v0.1.4
 - up to date jQuery dependency for Bower (#45).

v0.1.3
 - added explicit methods for each event (#28).

v0.1.2
 - fixed dragging across children of an element (#10).

v0.1.1
 - fixed `preventDefault` on desktop browsers for `tap` and `doubletap` events (#24).

v0.1.0
 - stable release.

v0.1.0-beta.2
 - fixed Chrome desktop and false positive touch detection.
 - fixed drag events with a correct end flag value (#17).

v0.1.0-beta.1 (buggy)
 - fixed successive taps to fail on different elements.

v0.1.0-beta (buggy)
 - better prevent default logic (#9, #12).
 - huge internal refactoring.

v0.1.0-alpha.1
 - give access to original events (#12).

v0.1.0-alpha
 - ie8 legacy support.
 - fixed prevent default event parameter.

v0.0.11
 - `press` event is now fired by `timeout` instead of `touchend`.

v0.0.10
 - fixed events fired multiple times (#1).
 - added `preventDefault` support.
 - internal refactoring for size and performance.

v0.0.9
  - fixed incorrect event type.
  - added to jam.
  - added to bower.

v0.0.8
  - fixed bugs on delegated events.
  - better cross-browser support (still needs some work/tests).
  - internal refactoring for consistency and performance.

v0.0.7
  - various cross browsers fixes.

v0.0.6
  - updated description.

v0.0.5
  - updated jquery manifest and published on http://plugins.jquery.com.

v0.0.4
  - added `drag` and `flick` gestures.
  - enhanced `event` object.
  - internal refactoring for consistency.

v0.0.3
  - migration to **grunt** 0.4.
  - migration to **mocha** / **chaijs** for tests.

v0.0.2
  - added `doubletap` and `press` gestures.
  - internal refactoring for consistency and performance.

v0.0.1
  - `tap` gesture first implementation.

Author

twitter/ngryman
Nicolas Gryman

More Repositories

1

reading-time

πŸ“š Medium's like reading time estimation.
TypeScript
1,369
star
2

cz-emoji

Commitizen adapter formatting commit messages using emojis.
JavaScript
356
star
3

badge-size

🍻 Displays the size of a given file in your repository.
JavaScript
298
star
4

gulp-bro

πŸ‘Š gulp + browserify + incremental build, done right.
JavaScript
121
star
5

social-redirects

🐳 Redirect glyph urls to your social networks profiles.
PHP
86
star
6

tree-crawl

πŸƒ Agnostic tree traversal library.
JavaScript
84
star
7

raf.js

πŸƒ Request Animation Frame polyfill
JavaScript
45
star
8

lol-champions

Simplified, up-to-date, League of Legends champions list.
JavaScript
45
star
9

ribs

πŸ– Responsive Images Baked Server-side.
JavaScript
39
star
10

fauda

Configuration made simple.
TypeScript
33
star
11

contributor-faces

Put your contributors faces in your readme.
JavaScript
31
star
12

virtual-pointer

Simulates a pointer with jQuery in PhantomJS and the browser.
JavaScript
25
star
13

codesandbox-theme

Codesandbox theme for VSCode.
22
star
14

obelisk-buildr

Isometric builder using obelisk.js.
JavaScript
21
star
15

compass

Compass wrapper and middleware for node.js.
JavaScript
16
star
16

lol-items

Simplified, up-to-date, League of Legends items list.
JavaScript
8
star
17

awesome-stars

A curated list of my GitHub stars!
7
star
18

tree-morph

πŸƒ Agnostic tree morphing library.
JavaScript
6
star
19

qs-numbers

A qs plugin that enables number parsing.
JavaScript
6
star
20

sass-dry

DRY your SASS code.
CSS
6
star
21

grunt-glue-js

Grunt task to build CommonJS modules for the browser using gluejs.
JavaScript
5
star
22

tree-mutate

πŸƒ n-ary tree mutation library.
JavaScript
4
star
23

lol-spells

Simplified, up-to-date, League of Legends summoner spells list.
JavaScript
4
star
24

v8-windows-tickprocessor

Drop-in v8 windows tick processor for profiling
4
star
25

readme-filename

Get a project readme file name.
JavaScript
3
star
26

mincmp

🍷 Compare npm package source files minimaps
JavaScript
3
star
27

woot-bar

Very minimalist tmux status bar that displays used memory and CPU usage.
Rust
3
star
28

experiment-car-drawing

Car Drawing Experiment
JavaScript
3
star
29

simulator

Mouse and touch events simulator
JavaScript
3
star
30

object-defaults

Like _.defaults, assigns properties of source objects to a target, without overriding existing ones.
JavaScript
2
star
31

hi5

Friendly lightweight type checker πŸ™Œ.
JavaScript
2
star
32

meta-dev

πŸ“¦ Meta package for devDependencies.
JavaScript
2
star
33

vinyl-adapter-picker

Protocol based vinyl adapter picker.
JavaScript
2
star
34

ds-linked-list

A simply linked list data structure in JavaScript.
JavaScript
2
star
35

optify

Generates a combination matrix from a set key/value pairs.
JavaScript
2
star
36

p-using

Dispose a resource when p-using promise resolves.
JavaScript
1
star
37

gulp-pimp

Pimp your imports!
JavaScript
1
star
38

ngryman.github.io

Script that executes me.
HTML
1
star
39

ViZion

OS for multimedia box in HTML and running on Node.JS
JavaScript
1
star
40

bookmarks-baby

Bookmarks Baby!
JavaScript
1
star
41

leap-impress

Control your Impress.js presentations with gestures using Leap Motion.
JavaScript
1
star
42

compare-values

Get functions to compare two values given an operator.
JavaScript
1
star
43

love-hate-particles

This is a little experiment about two particles' relationship.
CoffeeScript
1
star
44

thread-sass-loaders-bug

JavaScript
1
star
45

live-templates

A collection of handy live templates I use with IntelliJ / WebStorm.
1
star
46

npm-v

Get installed npm version
JavaScript
1
star
47

gulp-say

Text To Speech errors for gulp.
JavaScript
1
star
48

webstorm-libraries

Set of JavaScript for WebStorm and IntelliJ.
JavaScript
1
star
49

to-method

βš“ Convert c-like functions to class methods.
JavaScript
1
star
50

graceful-fs-stream

Graceful filesystem streams.
JavaScript
1
star
51

loop.js

HTML5 game ecosystem
JavaScript
1
star
52

traps

🐺 Capture me if you can!
JavaScript
1
star
53

lines-iterator

Iterates over lines, es6 way.
JavaScript
1
star
54

asmr

Show a list of your repos. That's it!
JavaScript
1
star
55

wombat-js

Javascript Game Engine
JavaScript
1
star
56

letenv

CLI environment variables loader.
JavaScript
1
star
57

unchain

Synchronous function chaining with delay support.
JavaScript
1
star
58

stores

Stores things efficiently. Don't worry about cache stampede anymore.
JavaScript
1
star
59

generator-library

Scaffold a generic node library.
JavaScript
1
star
60

compare-properties

Get functions to compare two object properties given an operator.
JavaScript
1
star