Git Product home page Git Product logo

doubletap.js's Introduction

doubleTap.js & xorTap.js

doubleTap.js

This utility emulates standard click and dblclick behavior for touch devices on typically non-clickable elements (e.g., an ordinary <div> element).

Unfortunately, touch browsers fire a broken click event which does not update the number of clicks making double click events much more difficult handle. As such, two new events are needed which gets consistently fired in both environments. These are implemented here as tap and dbltap.

These events are enabled for both mouse and touch devices by attaching a doubleTap handler:

// Enable tap & dbltap events
var myElem = document.getElementById('foo');
myElem.addEventListener(('ontouchend' in myElem) ? 'touchend' : 'mouseup', doubleTap(speed, distance), false);
myElem.addEventListener('tap', ontapHandler, false);
myElem.addEventListener('dbltap', ondbltapHandler, false);

xorTap.js

A related but different common problem is isolating clicks and double (or higher) clicks.

The xorTap.js script adds a helper for creating a combined event handler that fires these events mutually exclusively. It can be attached to the standard click event or the tap event that is created by doubleTap.js.

Just pass in handlers for each of the different levels (can even handle triple clicks and higher):

// Create a combo event handler
var xorhandler = xorTap(
	function(e) {
		alert('click!');
	},
	function(e) {
		alert('double click!');
	});

// Attach as click event
var myElem = document.getElementById('foo');
myElem.addEventListener('click', xorhandler, false);

// Or combine with doubleTap to attach as tap event
var myElem = document.getElementById('bar');
myElem.addEventListener(('ontouchend' in myElem) ? 'touchend' : 'mouseup', doubleTap(speed, distance), false);
myElem.addEventListener('tap', xorhandler, false);

Example

The example shows how these different pieces can work together to unify the experience for touch / mouse environments.

Note for iOS devices

Mobile Safari on iOS (iPhone / iPad) has a default behavior of zooming when double tapping. To disable zooming, a meta tag must be put in the head of the HTML document:

<meta name="viewport" content="width=device-width,user-scalable=no" />

MIT License

These scripts are free to use and are distributed under The MIT License.

Copyright (c)2012 Stephen M. McKamey.

doubletap.js's People

Contributors

mckamey avatar

Watchers

JT5D avatar 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.