How to install Hammer.js

Created

Hammer.js is a open-source library that can recognize gestures made by touch, mouse and pointerEvents. It doesn’t have any dependencies, and it’s small, only 7.34 kB minified + gzipped!

Installation

You can download the latest version of Hammer.js from the GitHub releases or use a Hammer.js CDN.

GitHub

Latest:2021-08-29 09:29:57
License:MIT License
Cloning with HTTPS URLs
git clone https://github.com/hammerjs/hammer.js.git
Cloning with SSH URLs
git clone [email protected]:hammerjs/hammer.js.git
Cloning with GitHub CLI
gh repo clone hammerjs/hammer.js

NPM

Version:2.0.8
Publish Time:2016-04-23 00:14:57
Unpacked Size:208.19 kB
Downloading and installing packages locally
npm i hammerjs
npm install hammerjs
yarn add hammerjs
Downloading and installing packages globally
npm i -g hammerjs
yarn global add hammerjs
Install Specific Version of a Package
npm install [email protected]
Bower
bower install hammerjs

For more information about Hammer.js see https://hammerjs.github.io/getting-started.

Usage

Code 1: - It’s easy to use, just include the library and create a new instance.

var hammertime = new Hammer(myElement, myOptions);
hammertime.on('pan', function(ev) {
  console.log(ev);
});

Code 2: - hammer.js has a quick start option for gestures it already recognizes.

// Get a reference to an element.
var square = document.querySelector('.square');

// Create an instance of Hammer with the reference.
var hammer = new Hammer(square);

// Subscribe to a quick start event: press, tap, or doubletap.
// For a full list of quick start events, read the documentation.
hammer.on('press', function(e) {
  e.target.classList.toggle('expand');
  console.log("You're pressing me!");
  console.log(e);
});