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.
Cloning a repository
You can clone a repository from GitHub.com to your local computer.
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
Install via NPM package
Run the following command to locally install the package and its dependencies with NPM.
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]
yarn add [email protected]
Bower
bower install hammerjs
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);
});