stackblitz
錄了個視頻玩
經過監聽這個流得知鼠標按下的時間,node
爲了達成思路,首先須要兩個流:dom
pushStart$ = new Subject();
pushOver$ = new Subject();
監聽鼠標按下,擡起事件。將上述兩個流產出。ide
@HostListener('mousedown') @HostListener('touchstart') pushStart() { this.pushStart$.next('start') console.log('start') } @HostListener('mouseup') @HostListener('mouseleave') @HostListener('touchend') pushOver(){ this.pushOver$.next('over') this.rd2.removeClass(this.el.nativeElement,'vibrate-1') }
加入監聽流的邏輯,修改pushStart代碼以下:動畫
@HostListener('mousedown') @HostListener('touchstart') pushStart() { this.pushStart$.pipe( tap(()=>{ this.rd2.addClass(this.el.nativeElement,'vibrate-1') }), switchMap(() => interval(1000)), tap(time => console.log(time)), takeUntil(this.pushOver$), filter(time => time === 1), take(1) ).subscribe(() => { console.log('done') this.rd2.removeClass(this.el.nativeElement, 'vibrate-1'); const node = this.rd2.parentNode(this.el.nativeElement) this.rd2.removeChild(node,this.el.nativeElement) this.delete.emit('done') }) this.pushStart$.next('start') console.log('start') }