Skip to content

onClickOutside

类别
导出体积
893 B
上次更改
2 minutes ago

监听元素外的点击事件。适用于模态框或下拉菜单等场景。

示例

用法

vue
<script setup>
import { onClickOutside } from '@vueuse/core'
import { ref } from 'vue'

const target = ref(null)

onClickOutside(target, event => console.log(event))
</script>

<template>
  <div ref="target">
    Hello world
  </div>
  <div>外部元素</div>
</template>

此函数使用 Event.composedPath(),不支持 IE 11、Edge 18 及以下版本。如果你的目标是这些浏览器,请在项目中包含此代码片段

组件用法

vue
<template>
  <OnClickOutside :options="{ ignore: [/* ... */] }" @trigger="count++">
    <div>
      点击我外面
    </div>
  </OnClickOutside>
</template>

指令用法

vue
<script setup lang="ts">
import { vOnClickOutside } from '@vueuse/components'
import { ref } from 'vue'

const modal = ref(false)
function closeModal() {
  modal.value = false
}
</script>

<template>
  <button @click="modal = true">
    打开模态框
  </button>
  <div v-if="modal" v-on-click-outside="closeModal">
    Hello World
  </div>
</template>

你还可以将处理程序设置为数组,以设置指令的配置项。

vue
<script setup>
import { vOnClickOutside } from '@vueuse/components'
import { ref } from 'vue'

const modal = ref(false)

const ignoreElRef = ref()

const onClickOutsideHandler = [
  (ev) => {
    console.log(ev)
    modal.value = false
  },
  { ignore: [ignoreElRef] },
]
</script>

<template>
  <button @click="modal = true">
    打开模态框
  </button>

  <div ref="ignoreElRef">
    点击外部忽略元素
  </div>

  <div v-if="modal" v-on-click-outside="onClickOutsideHandler">
    Hello World
  </div>
</template>

类型声明

typescript
export interface OnClickOutsideOptions extends ConfigurableWindow {
  /**
   * 不应触发事件的元素列表。
   */
  ignore?: MaybeRefOrGetter<(MaybeElementRef | string)[]>
  /**
   * 对内部事件侦听器使用捕获阶段。
   * @default true
   */
  capture?: boolean
  /**
   * 如果焦点移动到iframe,运行处理函数。
   * @default false
   */
  detectIframe?: boolean
}
export type OnClickOutsideHandler<
  T extends {
    detectIframe: OnClickOutsideOptions["detectIframe"]
  } = {
    detectIframe: false
  },
> = (
  evt: T["detectIframe"] extends true
    ? PointerEvent | FocusEvent
    : PointerEvent,
) => void
/**
 * 监听元素外部的点击事件。
 *
 * @see https://vueuse.org/onClickOutside
 * @param target 目标元素
 * @param handler 事件处理器
 * @param options 配置选项
 */
export declare function onClickOutside<T extends OnClickOutsideOptions>(
  target: MaybeElementRef,
  handler: OnClickOutsideHandler<{
    detectIframe: T["detectIframe"]
  }>,
  options?: T,
): () => void

源码

源码演示文档

贡献者

Anthony Fu
sibbng
一纸忘忧
Anthony Fu
wheat
Onion-L
Matej Černý
不见月
Doctorwu
Rory King
糠帅傅
Chestnut
vaakian X
Fiad
Young
Gavin
webfansplz
Jelf
JserWang
Alex Kozack

更新日志

v11.1.0 on 9/16/2024
9e598 - fix: improve cross-browser compatibility (#4185)
aa5e3 - fix: make ignore accept reactive values (#4211)
v10.6.0 on 11/9/2023
69851 - fix: adjust shouldListen handling timing (#3503)
v10.3.0 on 7/30/2023
9091e - fix: fix outside click on html element in ios (#3252)
v10.2.0 on 6/16/2023
2c66e - fix: ensure focus on iframe captured in firefox (#3066)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)
v9.8.0 on 12/20/2022
7b3db - feat: allow selector strings for ignore list (#2439)
12e21 - fix: apply ignore list on keyboard clicks (#2438)
v9.6.0 on 11/22/2022
ff96d - fix: call handler if click event is fired by a keypress (#2426)
v9.5.0 on 11/9/2022
f78c4 - fix: access correct document (#2404)
c913b - feat: support options in component (#2391)
v9.3.0 on 9/26/2022
5a976 - feat: add bubble modifier to directive (#2183)
a3742 - fix: put ignore logic on pointerdown event (#2198)