• Stars
    star
    659
  • Rank 65,949 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

Super lightweight script (~1kb) to detect via Javascript events like 'tap' 'dbltap' 'swipeup' 'swipedown' 'swipeleft' 'swiperight' on any kind of device.

Tocca.js

Build Status

NPM version NPM downloads

MIT License

CDNJS

Super lightweight script ( ~1kB ) to detect via Javascript events like 'tap' 'longtap' 'dbltap' 'swipeup' 'swipedown' 'swipeleft' 'swiperight' on any kind of device.

Demo

Donations

If this project helped getting your job done consider making a donation for all the time I spend to bring it to you

Donate

Installation

Npm

$ npm install tocca

Bower

$ bower install Tocca.js -save

Usage

Include the script into your page:

<script src="path/to/Tocca.js"></script>

Once you have included Tocca.js you will be able to catch all the new events:

elm.addEventListener('tap',function(e){});
elm.addEventListener('dbltap',function(e){});
elm.addEventListener('longtap',function(e){});
elm.addEventListener('swipeleft',function(e){});
elm.addEventListener('swiperight',function(e){});
elm.addEventListener('swipeup',function(e){});
elm.addEventListener('swipedown',function(e){});

It works with jQuery as well:

$(elm).on('tap',function(e,data){});
$(elm).on('dbltap',function(e,data){});
$(elm).on('longtap',function(e,data){});
$(elm).on('swipeleft',function(e,data){});
$(elm).on('swiperight',function(e,data){});
$(elm).on('swipeup',function(e,data){});
$(elm).on('swipedown',function(e,data){});

Tocca.js supports also the inline events if you are using Riot.js!

<div ontap="function(e){})"></div>
<div ondbltap="function(e){})"></div>
<div onlongtap="function(e){})"></div>
<div onswipeleft="function(e){})"></div>
<div onswiperight="function(e){})"></div>
<div onswipeup="function(e){})"></div>
<div onswipedown="function(e){})"></div>

API and Examples

Anytime you will use a Tocca.js event the callback function will receive a special event object containing the following properties

  • x { Int }: latest x position of pointer at the end of the event
  • y { Int }: latest y position of pointer at the end of the event
  • originalEvent { Object }: the original javascript native event that has been triggered
  • distance: this property is available only for the swipe events
  • x { Int }: the x absolute difference between the beginning and the end of the swipe gesture
  • y { Int }: the y absolute difference between the beginning and the end of the swipe gesture

Examples:

elm.addEventListener('dbltap',function (e){
  console.log(e.x);
  console.log(e.y);
});
elm.addEventListener('swipeup',function (e){
  console.log(e.x);
  console.log(e.y);
  console.log(e.distance.x);
  console.log(e.distance.y);
});

// with jQuery

$(elm).on('dbltap',function (e,data){
  console.log(data.x);
  console.log(data.y);
});
$(elm).on('swipeup',function (e,data){
  console.log(data.x);
  console.log(data.y);
  console.log(data.distance.x);
  console.log(data.distance.y);
});

Anyway you can combine Tocca.js with the default javascript touch events:

  • touchmove
  • touchstart
  • touchend
  • touchcancel

To disable the default touch behaviours (zoom on double tap, scroll on swipe...) on a certain element via javascript you can always use the following snippet:

elm.addEventListener('touchmove',function(e){e.preventDefault()});
elm.addEventListener('touchstart',function(e){e.preventDefault()});
elm.addEventListener('touchend',function(e){e.preventDefault()});

Configuration

Whenever you want to configure the plugin events settings you can do that simply specifying two constants before including Tocca.js into the page

<script>
var SWIPE_THRESHOLD = 100, // default value
  DBL_TAP_THRESHOLD = 200, // range of time in which a dbltap event could be detected,
  LONG_TAP_THRESHOLD = 1000, // range of time after which a longtap event could be detected
  TAP_THRESHOLD = 150, // range of time in which a tap event could be detected
  TAP_PRECISION = 60 / 2, // default value (touch events boundaries)
  JUST_ON_TOUCH_DEVICES = false, // default value ( decide whether you want to use the Tocca.js events only on the touch devices )
  IGNORE_JQUERY = false; // default value ( will not use jQuery events, even if jQuery is detected )
</script>
<script src="path/to/Tocca.js"></script>

In Tocca.js 1.1.0 you can also configure/get the internal options via function:

window.tocca({
  useJquery: your new option
  swipeThreshold: your new option
  tapThreshold: your new option
  dbltapThreshold: your new option
  longtapThreshold: your new option
  tapPrecision: your new option
  justTouchEvents: your new option
})

console.log(window.tocca()) // will always return the current internal options

Browser Support

Actually the script has been tested on all the modern browsers but it need a better testing phase over several platforms: Chrome 29+ Firefox 23+ Opera 12+ Safari 5+

It works on mobile/tablet browsers and on desktop browsers as well.

On the old browsers all the Tocca.js events cannot be triggered.

Changelog

2.0.9

  • fixed: removed const variables

2.0.8

  • fixed: 70

2.0.7

  • fixed: 69

2.0.6

  • update: improve multiple touches events detection

2.0.5

2.0.4

2.0.3

  • fixed: #51 #54
  • fixed: the PointerUp event is not always dispatched on Chrome and Android devices, prioritize always the touch* over pointer* events

2.0.1

2.0.0

1.1.0

  • added: the possibility to configure via function the internal tocca options
  • added: the IGNORE_JQUERY option

1.0.1

1.0.0

Thanks to @AndyOGo for his help on this release

  • fixed: #34 #35
  • fixed: more reliable and performant events on the hybrid devices
  • fixed: multiple events dispatched on tap

0.2.0

  • added: longtap event and the LONG_TAP_THRESHOLD variable
  • fixed: inconsistencies and issues across the hybrid devices

0.1.7

  • added: longtap event and the LONG_TAP_THRESHOLD variable

0.1.5

  • added: support for the inline events

0.1.4

  • fixed: dbltap #16
  • updated: node devDependecies updated

0.1.3

  • fixed: dbltap issue #14

0.1.2

  • updated: no more deferred tap events, a tap event gets triggered immediately if it's in the TAP_THRESHOLD time range
  • updated: a tap event will always come first a dbltap event

0.1.1

  • updated: better and faster tap events detection
  • updated: node devDependecies updated
  • added: DBL_TAP_THRESHOLD option

0.1.0

  • bugfix: optimizing the support for the microsoft hybrid devices (IE10/IE11)

0.0.8

  • 'touchcancel' event removed to fix and android issue on page scroll

0.0.7

  • nothing important (just a workaround to fix the tests on the motherfucker Phantomjs)

0.0.6

  • bugfix: do not set the mouse event listeners on any touch device and vice versa
  • added: new JUST_ON_TOUCH_DEVICES option to block all the Tocca.js events on the no-touch devices

0.0.5

  • tap precision option included

0.0.4

  • dpltap renamed dbltap
  • new demo added demo-fun.html

0.0.3

  • Tests added

0.0.2

  • Android Bug fix

What does Tocca mean?!

'Tocca' is the second person singular of the imperative Italian verb 'Toccare' that means to touch.

More Repositories

1

allora

Promisify using es6 Proxies every javascript API with less than 50 lines of code
JavaScript
679
star
2

Vague.js

Vague.js is an experimental script that allows you to blur any kind of html element thanks to the SVG filters
JavaScript
604
star
3

icaro

Smart and efficient javascript object observer, ideal for batching DOM updates (~1kb)
JavaScript
572
star
4

es6-project-starter-kit

Universal Starter Kit to build any javascript ES6 project/library runnable in nodejs and on any browser.
JavaScript
460
star
5

jQuery.BlackAndWhite

Clientside grayscale images on any browser
JavaScript
311
star
6

jquery.html5loader

jQuery.html5Loader can preload images, SVGs, html5 video and audio sources, scripts, css, and text files.
JavaScript
302
star
7

parallax

ES6/ES2015 HW accelerated scrollable images parallax
JavaScript
276
star
8

go-observable

It allows you to send and receive events with a tiny simple API
Go
240
star
9

nodejs-MySQL-push-notifications-demo

Node server that is able to read from a MySql database and then stream those data via websocket to many client connected on the same page
JavaScript
106
star
10

nodejs-push-notification-server

Simple nodejs push notification server using socket.io
JavaScript
76
star
11

riot-app-example

Simple riotjs isomorphic app
JavaScript
76
star
12

animore

1kb script that will make your DOM state transitions smoother & easier
JavaScript
72
star
13

erre

Modern, performant and tiny (~0.5kb) streams script using generators for browsers and node.js
JavaScript
30
star
14

nodejs-arduino-example

Simple example to show how to use arduino to communicate digital signals to a nodejs server
JavaScript
25
star
15

cmt

Get rid of all the boring git commit messages like 'small fix'. cmt is an automatic git commit generator
JavaScript
18
star
16

site-under-construction

Simple splash page to use while a site is still under construction
CSS
17
star
17

fcf

Monadic Functional Control Flow Micro-Library for Javascript/Typescript
TypeScript
14
star
18

rawth

Pure functional isomorphic router based on streams
JavaScript
14
star
19

fortytwo

List of interactive prompt helpers to pimp the UIs of your haskell programs
Haskell
12
star
20

reactive-libs-bench

Benchmark tests to check some popular reactive libraries - raw tests no DOM involved
JavaScript
11
star
21

days-since-last-javascript-drama

Timer to track how many days passed since the latest Javascript drama
HTML
9
star
22

riot-preset-babel-test

JavaScript
8
star
23

threejs-amd-bootstrap

Simple and lite threejs bootstrap project build using requirejs
JavaScript
7
star
24

nisiu

Password manager written in Riot.js
HTML
6
star
25

i-got-ya

Stop fucking with my machine when I am not here :)
Go
6
star
26

the-cost-of-native-javascript-modules

The performance cost of native javascript modules
HTML
5
star
27

immutable-dom

Lock any HTML DOM Element 🔐
JavaScript
4
star
28

jquery-fitToParent

A jQuery plugin for proportionally scaling an element to its parent element. Useful for thumbnails and avatars.
JavaScript
4
star
29

ruit

Functional tasks serialization mini script (0.3kb)
JavaScript
4
star
30

package-requirejs

Generate a valid requirejs-config.js file automatically detecting all the package.json dependencies
JavaScript
4
star
31

riot-now-demo

Simple now riot component
3
star
32

Caronte.js

Simple javascript ajax file uploader script made for the modern browsers
JavaScript
3
star
33

EasyAnimationFrame

Using EasyAnimationFrame.js you can make html5 canvas or html animations having the complete control over the framerate.
JavaScript
3
star
34

riot-chrome-extension

riojs chrome extension example
JavaScript
3
star
35

rob

Personal projects generator written in haskell
Haskell
3
star
36

smell-of-funtional-programming

Demo code for the "Smell Of Functional Programming" presenation
JavaScript
3
star
37

SublimeText2-Parallel-Builder-Plugin

Sublime Text2 Plugin - It allows you to run more than one build command in Parallel
Python
3
star
38

gianlucaguarini.github.io

gianlucaguarini.github.io original source
HTML
3
star
39

cumpa

Minimal function composition implementation
JavaScript
2
star
40

riot-ssr-now

Simple riot example to demonstrate how to deploy on zeit.co
JavaScript
2
star
41

curri

Minimal curry implementation
JavaScript
2
star
42

mail-client-ui-demo

Simple Mail Client Demo with ~20 Javascript code
JavaScript
2
star
43

grunt-testardo

grunt plugin to test your project using testardo
JavaScript
2
star
44

html5-semantic

Meaningful HTML5 Designs - webzurich - 25.05.2020
2
star
45

the-colour-of-the-moment

Random colored image generator service written with go
Riot
2
star
46

executor

Haskell module to execute single or multiple shell commands
Haskell
1
star
47

amp-vs-pwa-vs-HTML

Simple Benchmark to check the speed of 3 different website types
HTML
1
star
48

the-fly

JavaScript
1
star
49

OSXKeyboard

Go helper script to detect the global keypress events on OSX
C
1
star