What is the best way to implement swipe navigation in Angular 2?

后端 未结 4 1546
[愿得一人]
[愿得一人] 2020-12-12 23:24

I\'m new to Angular 2 and am looking for a way to implement a good tab touch swipe navigation for mobile users with a swipe transition to the next tab view.

So far I

4条回答
  •  感情败类
    2020-12-13 00:08

    For the swipe detection here is a simpler solution than adding HammerJS:

    In app.component.html:

    App content here

    In app.component.ts:

    private swipeCoord?: [number, number];
    private swipeTime?: number;
    
    swipe(e: TouchEvent, when: string): void {
      const coord: [number, number] = [e.changedTouches[0].clientX, e.changedTouches[0].clientY];
      const time = new Date().getTime();
    
      if (when === 'start') {
        this.swipeCoord = coord;
        this.swipeTime = time;
      } else if (when === 'end') {
        const direction = [coord[0] - this.swipeCoord[0], coord[1] - this.swipeCoord[1]];
        const duration = time - this.swipeTime;
    
        if (duration < 1000 //
          && Math.abs(direction[0]) > 30 // Long enough
          && Math.abs(direction[0]) > Math.abs(direction[1] * 3)) { // Horizontal enough
            const swipe = direction[0] < 0 ? 'next' : 'previous';
            // Do whatever you want with swipe
        }
      }
    }
    

    Note: I tried the HammerJS solution but configuring it to ignore mouse gestures was impossible because you don't have direct access to the Hammer object. So selecting some text was forcing navigation to the next page...

提交回复
热议问题