angular - 在 Angular 2 中实现滑动导航的最佳方式是什么?

标签 angular navigation swipe-gesture

我是 Angular 2 的新手,正在寻找一种方法来为移动用户实现良好的标签触摸滑动导航,并通过滑动过渡到下一个标签 View 。

到目前为止,我找到了一个名为 angular2-useful-swiper 的包尽管我并不热衷于使用它,因为我最终会尽早初始化我的组件,即使它们不在 View 中也是如此。

有人知道为 Angular 2 实现基于选项卡滑动的导航的好方法吗?任何反馈将不胜感激。 :)

最佳答案

对于滑动检测,这里是一个比添加 HammerJS 更简单的解决方案:

在 app.component.html 中:

<div (touchstart)="swipe($event, 'start')" (touchend)="swipe($event, 'end')">
  App content here
</div>

在 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
    }
  }
}

注意:我尝试了 HammerJS 解决方案,但无法将其配置为忽略鼠标手势,因为您无法直接访问 Hammer 对象。所以选择一些文本会强制导航到下一页...

关于angular - 在 Angular 2 中实现滑动导航的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592156/

相关文章:

angular - Angular和Docker:无法从主机访问应用程序

javascript - 未处理的 promise 拒绝 : Zone. js 已检测到 ZoneAwarePromise `(window|global).Promise` 已被覆盖

angular - 如何在 Angular 中为 prod 和 test 设置不同的 dist 文件夹

html - 页脚低于固定页脚

html - CSS Flexbox 适用于除 Safari 以外的所有浏览器?

android - 使用 RecyclerView 在列表中滑动以删除

Angular 编码散列片段 : How to prevent it?

javascript - 当使用 Javascript 选择链接时,如何隐藏 HTML 元素?

flutter - 如何检测 flutter 中的滑动

Android - 对未正确填充的数据禁用向前滑动