Mouse swipe controls in Angular 9 using HammerJS Last Updated : 19 Aug, 2020 Comments Improve Suggest changes Like Article Like Report Angular is an application design framework and development platform for creating efficient and sophisticated single-page apps. It has been changed a lot from its first release. And it keeps adding new features and modifications in its releases which is a good thing. But sometimes what we have used in the previous version stops working in the latest version. Same is the case with HammerJS. HammerJS is a very good open-source library that can recognise gestures made by touch, mouse and pointer events. You can read more about HammerJS and its documentation here. In Angular 9, if you use previous methods of adding HammerJS, it will not work because Angular has modified some of its features. So you will go through the whole process of working with HammerJS in Angular 9 from starting. Approach: The approach is to install the hammerjs package locally, import it in main.ts and set the Hammer gesture configuration by extending the HammerGestureConfig class. Then you can bind to specific events like swipe, pan, pinch, press, etc. The most important thing is to import the HammerModule in app module file. Example and Explanation: Swipe Gesture In your angular project, install the hammerjs package locally by running the below command. npm install --save hammerjs Now, you will need to import the hammerjs module in your main.ts file. If you do not import this, you will get an error in your console. Error: Hammer.js is not loaded, can not bind to XYZ event. import 'hammerjs'; Let us move on to our app.module.ts, here you can add your own configuration of Hammer gestures using HammerGestureConfig class and HAMMER_GESTURE_CONFIG like in the below image. Also make sure to import HammerModule as it is the current modification which has been done in Angular 9. Otherwise your project will not work and will not give error too. ( Although this is in typescript, but the editor does not support that yet so ignore and do not get confused. ) app.module.ts javascript // add this in your app.module.ts import { NgModule, Injectable } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; // particular imports for hammer import * as Hammer from 'hammerjs'; import { HammerModule, HammerGestureConfig, HAMMER_GESTURE_CONFIG} from '@angular/platform-browser'; import { AppComponent } from './app.component'; @Injectable() export class MyHammerConfig extends HammerGestureConfig { overrides = <any> { swipe: { direction: Hammer.DIRECTION_ALL }, }; } @NgModule({ imports: [ BrowserModule, FormsModule, HammerModule ], declarations: [ AppComponent, HelloComponent ], bootstrap: [ AppComponent ], providers: [ { provide: HAMMER_GESTURE_CONFIG, useClass: MyHammerConfig, }, ], }) export class AppModule { } Now we will create our simple example for Swipe gestures. For app.component.html, you can add the following code. app.component.html html <!--add this in your app.component.html --> <div class="swipe" (swipe)="onSwipe($event)"> <h1>Swipe Gesture</h1> <p>Works with both mouse and touch.</p> <h5 [innerHTML]="direction"></h5> </div> Add some styles to your example like this in app.component.css. The important thing to notice is where you want the swipe gesture, set user-select as none. app.component.css css .swipe { background-color: #76b490; padding: 20px; margin: 10px; border-radius: 3px; height: 500px; text-align: center; overflow: auto; color: rgb(78, 22, 131); user-select: none; } h1, p { color: rgb(116, 49, 11); } Lastly, add your typescript code in app.component.ts like this. javascript // add this in your app.component.ts import { Component } from "@angular/core"; @Component({ selector: "my-app", templateUrl: "./app.component.html", styleUrls: ["./app.component.css"] }) export class AppComponent { direction = ""; onSwipe(event) { const x = Math.abs( event.deltaX) > 40 ? (event.deltaX > 0 ? "Right" : "Left") : ""; const y = Math.abs( event.deltaY) > 40 ? (event.deltaY > 0 ? "Down" : "Up") : ""; this.direction += `You swiped in <b> ${x} ${y} </b> direction <hr>`; } } Output: There are several other gestures you can implement using HammerJS just like that. For more information, please read their documentation. Comment More infoAdvertise with us Next Article Mouse swipe controls in Angular 9 using HammerJS I ishusinghal03 Follow Improve Article Tags : JavaScript Web Technologies AngularJS AngularJS-Misc Similar Reads JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML 14 min read What is an API (Application Programming Interface) In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms.What is an API?An API is a set of 10 min read Introduction of Firewall in Computer Network A firewall is a network security device either hardware or software-based which monitors all incoming and outgoing traffic and based on a defined set of security rules it accepts, rejects, or drops that specific traffic. It acts like a security guard that helps keep your digital world safe from unwa 10 min read Like