我是Angular 2的新用户,我正在寻找一种方法,为移动用户实现一个良好的标签触摸滑动导航,并滑动过渡到下一个标签视图。
到目前为止,我找到了一个名为angular2-useful-swiper的软件包,虽然我并不热衷于使用它,因为我最终会尽早初始化我的组件,即使它们不在视野中。
有没有人知道为Angular 2实现基于标签滑动的导航的好方法?任何反馈将不胜感激。 :)
答案 0 :(得分:46)
对于滑动检测,这里比添加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].pageX, e.changedTouches[0].pageY];
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对象。因此选择一些文本迫使导航到下一页......
答案 1 :(得分:5)
您可以使用HammerJS
来实施触摸操作,例如,您可以按照plunker进行操作。
包含hammer.js文件
<script src="https://cdnjs.cloudflare.com/ajax/libs/hammer.js/2.0.8/hammer.js"></script>
或
npm install hammerjs --save
对于使用hammerjs的浏览器触控支持,请
<script src="http://cdn.rawgit.com/hammerjs/touchemulator/master/touch-emulator.js"></script>
<script>
在app.module.ts中导入
import { HammerGestureConfig, HAMMER_GESTURE_CONFIG } from '@angular/platform-browser';
export class MyHammerConfig extends HammerGestureConfig {
overrides = <any>{
'swipe': {velocity: 0.4, threshold: 20} // override default settings
}
}
@NgModule({
imports: [BrowserModule],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [{
provide: HAMMER_GESTURE_CONFIG,
useClass: MyHammerConfig
}] // use our custom hammerjs config
})
要实施标签angular2-material
是一个很好的起点,请关注this link
答案 2 :(得分:3)
首先安装hammerjs和动作触摸动作polyfill:
$ npm install hammerjs hammer-timejs
然后将导入添加到'app.module.ts',以便它们将被使用/捆绑:
import 'hammerjs';
import 'hammer-timejs';
现在您可以处理动作的事件:
旋转
捏
按
潘
点击
滑动
例如,您可以说:
<li *ngFor="let employee of employeesList;" (swiperight)="myswiperight(employee)" (swipeleft)="myswipeleft(employee)">
或者:
<div (panstart)="onPanStart($event)" (panmove)="onPan($event)">
答案 3 :(得分:1)
我根据@Elvis Metodiev 的回答和@pikiou 创建了一个指令:
<块引用>swipe.directive.ts
import { Directive, EventEmitter, HostListener, Output } from '@angular/core';
@Directive({ selector: '[swipe]' })
export class SwipeDirective {
@Output() next = new EventEmitter<void>();
@Output() previous = new EventEmitter<void>();
swipeCoord = [0, 0];
swipeTime = new Date().getTime();
constructor() { }
@HostListener('touchstart', ['$event']) onSwipeStart($event) {
this.onSwipe($event, 'start');
}
@HostListener('touchend', ['$event']) onSwipeEnd($event) {
this.onSwipe($event, 'end');
}
onSwipe(e: TouchEvent, when: string) {
this.swipe(e, when);
}
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 swipeDir = direction[0] < 0 ? 'next' : 'previous';
if (swipeDir === 'next') {
this.next.emit();
} else {
this.previous.emit();
}
}
}
}
}
<块引用>
tour.component.component.ts
<div
...
swipe
(next)="onRotateNext()"
(previous)="onRotatePrevious()"
>
...
</div>
答案 4 :(得分:0)
我设法提出了一种“随处可写”类型的函数,我将其放置在名为“ gestures”的目录中,然后创建了一个名为“ swipe.ts”的文件,并将其放入其中。
let swipeCoord = [0, 0];
let swipeTime = new Date().getTime();
export function 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') {
swipeCoord = coord;
swipeTime = time;
} else if (when === 'end') {
const direction = [coord[0] - swipeCoord[0], coord[1] - swipeCoord[1]];
const duration = time - swipeTime;
if (duration < 1000 //
&& Math.abs(direction[0]) > 30 // Long enough
&& Math.abs(direction[0]) > Math.abs(direction[1] * 3)) { // Horizontal enough
const swipeDir = direction[0] < 0 ? 'next' : 'previous';
if (swipeDir === 'next') {
alert('swipe next');
} else {
alert('swipe prev');
}
}
}
}
然后导入所需的组件,如下所示:
import {swipe} from '../../gestures/swipe';
并创建一个名为:
的函数onSwipe(e: TouchEvent, when: string) {
swipe(e, when);
}
在所需组件的HTML中,执行以下操作:
<div (touchstart)="onSwipe($event, 'start')"
(touchend)="onSwipe($event, 'end')">
<!-- whatever content you have goes here -->
</div>
PS-信用归@pikiou所有。我只是想出了更高的抽象水平,对我而言,这更有意义。