Giter Club home page Giter Club logo

tocca.js's Introduction

Tocca.js

Build Status

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

Demo

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('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('swipeleft',function(e,data){});
$(elm).on('swiperight',function(e,data){});
$(elm).on('swipeup',function(e,data){});
$(elm).on('swipedown',function(e,data){});

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_TRESHOLD = 80, // default value
	TAP_TRESHOLD = 200, // default value
	TAP_PRECISION = 60; // default value (touch events boundaries)
</script>
<script src="path/to/Tocca.js"></script>

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

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.

tocca.js's People

Contributors

gianlucaguarini avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.