-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): add MouseSwipeDetector and TouchSwipeDetector classes
Implemented MouseSwipeDetector and TouchSwipeDetector in swiperia-js, extending AbstractSwipeDetector. These classes handle swipe detection using mouse events and touch events respectively. Each class includes methods to initialize event listeners (`listen`), handle event start, move, and end (`_start`, `_move`, `_end`), and clean up listeners (`destroy`), providing specific point calculation for their event types.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Point, SwipeCallback, SwipeConfig } from 'swiperia-core'; | ||
import { AbstractSwipeDetector } from './AbstractSwipeDetector'; | ||
|
||
export class MouseSwipeDetector extends AbstractSwipeDetector { | ||
constructor(el: HTMLElement, config?: SwipeConfig) { | ||
super(el, config); | ||
} | ||
|
||
point(e: MouseEvent): Point { | ||
return [e.pageX, e.pageY]; | ||
} | ||
|
||
listen(callback: SwipeCallback): void { | ||
this._callback = callback; | ||
this.el.addEventListener('mousedown', this._start, false); | ||
this.el.addEventListener('mousemove', this._move, false); | ||
this.el.addEventListener('mouseup', this._end, false); | ||
} | ||
|
||
destroy(): void { | ||
this.el.removeEventListener('mousedown', this._start, false); | ||
this.el.removeEventListener('mousemove', this._move, false); | ||
this.el.removeEventListener('mouseup', this._end, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Point, SwipeCallback, SwipeConfig } from 'swiperia-core'; | ||
import { AbstractSwipeDetector } from './AbstractSwipeDetector'; | ||
|
||
export class TouchSwipeDetector extends AbstractSwipeDetector { | ||
constructor(el: HTMLElement, config?: SwipeConfig) { | ||
super(el, config); | ||
} | ||
|
||
point(e: TouchEvent): Point { | ||
const touch = e.touches[0]; | ||
return [touch.pageX, touch.pageY]; | ||
} | ||
|
||
listen(callback: SwipeCallback): void { | ||
this._callback = callback; | ||
this.el.addEventListener('touchstart', this._start, false); | ||
this.el.addEventListener('touchmove', this._move, false); | ||
this.el.addEventListener('touchend', this._end, false); | ||
} | ||
|
||
destroy(): void { | ||
this.el.removeEventListener('touchstart', this._start, false); | ||
this.el.removeEventListener('touchmove', this._move, false); | ||
this.el.removeEventListener('touchend', this._end, false); | ||
} | ||
} |