Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

拖动和点击同时触发解决办法 #92

Open
deepthan opened this issue Sep 10, 2020 · 0 comments
Open

拖动和点击同时触发解决办法 #92

deepthan opened this issue Sep 10, 2020 · 0 comments
Labels

Comments

@deepthan
Copy link
Owner

问题

拖拽的时候会触发点击事件,所以得分清是拖拽还是点击

原理介绍

  • 点击一般使用click,但是我们这里需要用mouseDownmouseUp来实现
  • 拖动过程中会触发三个事件
    • mouseDownmouseMovemouseUp

所以我们在mouseDown事件中记录鼠标的位置,在mouseUp 时比较此时的位置是否相同则可判断是点击还是拖拽。

html

<div
  cdkDrag
  (mousedown)="onMousedown($event)"
  (mouseup)="onMouseup($event)"
>
</div>

ts

  dowmPosi = [];
  upPosi = []

  onMousedown(event: MouseEvent){
    const { pageX, pageY } = event;
    this.dowmPosi.push(pageX, pageY);
  }

  onMouseup(event: MouseEvent){
    const { pageX, pageY } = event;
    this.upPosi.push(pageX, pageY)
    if(_.isEqual(this.dowmPosi, this.upPosi)){
      // 这里去执行点击事件做的事情
    }
    this.resetMouseFlag();
  }

  resetMouseFlag(){
    this.dowmPosi = [];
    this.upPosi = [];
  }
@deepthan deepthan added the event label Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant