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') }