element分页点击事件_click 事件规范知多少

8641e8eb524fa51e9a203d9f57fe4ae1.png

本文主要科普 mouseevent 的规范:

Each implementation will determine the appropriate hysteresis tolerance, but in general SHOULD fireclickanddblclickevents when the event target of the associatedmousedownandmouseupevents is the same element with nomouseoutormouseleaveevents intervening, and SHOULD fireclickanddblclickevents on the nearest common inclusive ancestor when the associatedmousedownandmouseupevent targets are different.

重点关注最后一句:当 mousedown 事件对象是 A, mouseup 的事件对象是 B, click 事件会在 A 和 B 共同的祖先节点触发。

先来看看一个 issue:鼠标在 Dialog 内部按下,滑动到 Dialog 外部时松开,Dialog 会关闭。

[Bug Report] 【el-dialog】close bug · Issue #15000 · ElemeFE/element

来看一下 Dialog 的代码,关闭 Dialog 的行为是在 handleWrapperClick 这个方法里面做的,绑定这个事件的时候发生

<div class="el-dialog__wrapper" v-show="visible" @click.self="handleWrapperClick">
<script>
export default {
  methods: {
    handleWrapperClick() {
      if (!this.closeOnClickModal) return;
      this.handleClose();
    }
  }
}
</script>

组件中使用了 vue 的事件修饰符 .self ,目的不希望点击事件在 <div class"el-dialog__wrapper"> 节点的子节点触发的时候也关闭弹出框,如下图所示,点击弹窗内容的时候没有触发 handleWrapperClick, 点击遮罩层的时候触发了 handleWrapperClick 事件关闭了弹窗 。

171f631733720b94372cc0bfb6119617.gif

这个写法对于常用操作是没有问题的,但是如果鼠标在 Dialog 内部按下,滑动到 Dialog 外部时松开,会触发 handleWrapperClick 方法。 原因是:mousedown 事件对象是 A( Dialog 内部的节点), mouseup 的事件对象是B(遮罩层),click 事件会在 A 和 B 共同的祖先节点触发, 他们共同的节点是 B。

bfe46af7e51c24b36850f3e60b76c5bb.gif

参考:

https://www.w3.org/TR/uievents/#events-mouseevent-event-order

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值