前端事件可穿透与禁止穿透

10 篇文章 0 订阅
8 篇文章 0 订阅

在开发过程中遇到一些问题,就是在一个弹窗上面有对应的输入框,在输入框中输入空格,然后其他绑定全局的空格事件就被触发了,也就是把弹窗下面的事件被触发了,那么如何有效的阻止事件的事件透传触发呢?很简单就是提供对应事件的.stop就行了。

比如我是弹窗的键盘事件,那么我就在modal上面增加@keydown.space.stop

.stop事件修饰符就是封装了 event.stopPropagation()的逻辑在里面,也就是快速帮助你去阻止冒泡。

<n-modal
    :show="visible"
    :display-directive="displayDirective"
    :style="$props.largeSize && { height: '900px' }"
    :auto-focus="autoFocus"
    :mask-closable="maskClosable"
    :unstable-show-mask="showMask"
    @update:show="$emit('update:visible', false)"
    // 阻止keydown事件冒泡
    @keydown.space.stop
    // 阻止keyup事件冒泡
    @keyup.space.stop
  >
  //....弹窗内容
</n-modal>

当然还有一些其他事件修饰符比如.prevent.space.shift等等,更多的内容查看vue3事件部分

简单看下Vue3部分的源码吧。

// 声明拦截的描述符(其中就包括我们刚刚使用的stop,其实就是执行了一句e.stopPropagation())
const modifierGuards: Record<
  string,
  (e: Event, modifiers: string[]) => void | boolean
> = {
  stop: e => e.stopPropagation(),
  prevent: e => e.preventDefault(),
  self: e => e.target !== e.currentTarget,
  ctrl: e => !(e as KeyedEvent).ctrlKey,
  shift: e => !(e as KeyedEvent).shiftKey,
  alt: e => !(e as KeyedEvent).altKey,
  meta: e => !(e as KeyedEvent).metaKey,
  left: e => 'button' in e && (e as MouseEvent).button !== 0,
  middle: e => 'button' in e && (e as MouseEvent).button !== 1,
  right: e => 'button' in e && (e as MouseEvent).button !== 2,
  exact: (e, modifiers) =>
    systemModifiers.some(m => (e as any)[`${m}Key`] && !modifiers.includes(m))
}
export const withModifiers = (fn: Function, modifiers: string[]) => {
  return (event: Event, ...args: unknown[]) => {
    for (let i = 0; i < modifiers.length; i++) {
      const guard = modifierGuards[modifiers[i]]
      // 拦截到对应的描述符,然后先执行
      if (guard && guard(event, modifiers)) return
    }
    return fn(event, ...args)
  }
}

如果用 JS 的话,直接就使用e.stopPropagation()就行。

//html
<button class="button">关闭</button>
const button = document.querySelector('[class=button]');
button.onClick = (e) => {
  e.stopPropagation();
  // ...
}

还有一种情况就是,设计非要把蒙层放在最上层,明明遮住了按钮,他不管,他就觉得这么放好看,那怎么办?好在前端足够强大,能够解决这个问题,就是css中的一个pointer-events

请添加图片描述

除了none/auto以外,其他都是用在svg项目中。

<!DOCTYPE html>
<html>
<head>
  <title>阻止点击穿透/实现点击穿透</title>
  <style>
    .parent{
      width: 300px;
      height: 150px;
      padding: 20px;
      margin: 20px;
      background: #fff;
    }
    .child{
      width: 50px;
      height: 50px;
      line-height: 50px;
      border-radius: 10px;
      background: red;
      text-align: center;
      color: #fff;
    }
    .non-click{
      pointer-events: none;  /* 上层加上这句样式可以实现点击穿透 */
    }
  </style>
</head>
<body>

  <div class="parent" οnclick="parent()">
    <p>阻止点击穿透</p>
    <div class="child" οnclick="child()">试试</div>
  </div>

  <div class="parent" οnclick="parent()">
    <p>点击穿透</p>
    <div class="child non-click" οnclick="child()">试试</div>
  </div>
  <script>
    const parent = function() {
      alert("点击下层")
    }
    const child = function() {
      event.stopPropagation();    // 上层加上这行代码,可以阻止点击穿透
      alert("点击上层")
    }
  </script>
</body>
</html>

请添加图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现图表的穿透效果,可以使用 ECharts 提供的点击事件和处理函数来实现。具体步骤如下: 1. 首先,为图表添加点击事件监听器。可以通过调用 `chart.on('click', handler)` 方法来实现,其中 `chart` 是你创建的 ECharts 实例,`handler` 是点击事件的处理函数。 2. 在点击事件处理函数中,可以获取到点击的坐标位置以及点击的数据信息。可以通过 `params` 参数来获取这些信息。例如,`params.dataIndex` 可以获取到点击的数据索引,`params.value` 可以获取到点击的数据值等。 3. 根据点击的数据信息,可以进行相应的处理逻辑,比如打开一个新的弹窗或者展示更详细的信息等。 以下是一个简单的示例代码,展示了如何实现图表的穿透效果: ```javascript // 创建 ECharts 实例 var chart = echarts.init(document.getElementById('chart')); // 为图表添加点击事件监听器 chart.on('click', function(params) { // 获取点击的数据信息 var dataIndex = params.dataIndex; var value = params.value; // 根据需要进行相应的处理逻辑 // ... // 示例:在控制台打印点击的数据信息 console.log('点击的数据索引:', dataIndex); console.log('点击的数据值:', value); }); // 设置图表的配置项和数据 var option = { // 图表的配置项 // ... // 图表的数据 series: [{ // 数据系列的配置项 // ... // 数据 data: [10, 20, 30, 40, 50] }] }; // 使用刚指定的配置项和数据显示图表 chart.setOption(option); ``` 以上代码中,通过调用 `chart.on('click', handler)` 来为图表添加点击事件监听器,并在处理函数中获取点击的数据信息。你可以根据需要自行修改处理逻辑,实现图表的穿透效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值