美文网首页
鼠标滑入滑出防抖

鼠标滑入滑出防抖

作者: 姜治宇 | 来源:发表于2023-03-14 14:13 被阅读0次

    鼠标滑入滑出,可以利用rxjs的debounceTime做防抖,延时期间的一切动作都是无效的,比如迅速的滑入滑出,结果是一次都不执行。
    鼠标滑入滑出的动作,可以结合Subject消息通知机制,尽量不要直接操作dom。

    import { Subject } from 'rxjs';
    import { debounceTime } from 'rxjs/operators';
    @Component({
      selector: 'app-demo',
      templateUrl: './demo.component.html',
      styleUrls: ['./demo.component.scss'],
    })
    export class demoComponent{
      public showFuncDetail$ = new Subject();
      private showFuncDetailSub$:any;
      constructor() {
        //鼠标划入划出
        this.showFuncDetailSub$ = this.showFuncDetail$.pipe(debounceTime(500)).subscribe((v:any) => { 
          if(v){
            this.mouseShowNode(v.event,v.node);
          } else {
            this.mouseHideNode();
          }
          
        });
      }
      ngOnDestroy(): void {
        this.showFuncDetailSub$.unsubscribe();
      }
      mouseEnterNode(event,node){
        this.showFuncDetail$.next({event:event,node:node});
      }
      mouseOutNode(){
        this.showFuncDetail$.next(null);
      }
      mouseShowNode(event,node){
        this.topDis = event.pageY;
        this.leftDis = event.pageX;
        this.selectNode(node,false);
        this.isShowPop = true;
      }
      mouseHideNode(){
        this.isShowPop = false;
      }
    }
    //html
    <div (mouseenter)="mouseEnterNode($event,node)" (mouseleave)="mouseOutNode()">
      ...
    </div>
           
    

    相关文章

      网友评论

          本文标题:鼠标滑入滑出防抖

          本文链接:https://www.haomeiwen.com/subject/fgxkrdtx.html