Skip to content

Instantly share code, notes, and snippets.

@kawinpop
Created November 29, 2019 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kawinpop/e5bf1eab3cada7e4a38eab709fb941ee to your computer and use it in GitHub Desktop.
Save kawinpop/e5bf1eab3cada7e4a38eab709fb941ee to your computer and use it in GitHub Desktop.
import { Directive, HostListener, Output, EventEmitter, Input } from '@angular/core';
@Directive({
selector: '[ng-touch]'
})
export class TouchDirective {
@Output()
touch = new EventEmitter();
@Input()
timemove: number;
countTouchMove = 0;
@HostListener('click', ['$event']) onclick($event) {
if (event.cancelable) {
event.preventDefault();
}
}
@HostListener('touchstart', ['$event']) ontouchstart($event) {
if (this.timemove === 0) {
this.touch.emit();
}
}
@HostListener('touchmove', ['$event']) ontouchmove($event) {
this.countTouchMove++;
}
@HostListener('touchend', ['$event']) ontouchend($event) {
if (event.cancelable) {
event.preventDefault();
event.stopPropagation();
}
const time = this.timemove || 3;
if (this.countTouchMove < time) {
this.touch.emit();
}
this.countTouchMove = 0;
}
constructor(
) { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment