指针事件

使用 pointer-events 工具类控制元素如何响应鼠标和触摸交互。

ClassStyles
.pe-nonepointer-events: none;
.pe-autopointer-events: auto;

概述

Bootstrap 提供 .pe-none 和 .pe-auto 类,通过 pointer-events CSS 属性来阻止或添加元素交互。

基本用法

This link can not be clicked.

This link can be clicked (this is default behavior).

This link can not be clicked because the pointer-events property is inherited from its parent. However, this link has a pe-auto class and can be clicked.

HTML
<p><a href="#" class="pe-none" tabindex="-1" aria-disabled="true">This link</a> can not be clicked.</p>
<p><a href="#" class="pe-auto">This link</a> can be clicked (this is default behavior).</p>
<p class="pe-none"><a href="#" tabindex="-1" aria-disabled="true">This link</a> can not be clicked because the <code>pointer-events</code> property is inherited from its parent. However, <a href="#" class="pe-auto">this link</a> has a <code>pe-auto</code> class and can be clicked.</p>

重要注意事项

.pe-none 类(及其所设置的 pointer-events CSS 属性)仅阻止与指针(鼠标、触控笔、触摸)的交互。默认情况下,带有 .pe-none 的链接和控件对键盘用户仍然可聚焦且可操作。

要确保它们即使对键盘用户也完全无效,你可能需要添加额外属性,例如:

  • tabindex="-1"(阻止它们获得键盘焦点)
  • aria-disabled="true"(向辅助技术传达它们实际上已被禁用的信息)
  • JavaScript 以完全阻止它们可被操作

更好的替代方案

如有可能,更简单的解决方案是:

  • 对于表单控件:添加 disabled HTML 属性
  • 对于链接:移除 href 属性,使其成为不可交互的锚点或占位链接

无障碍警告: 使用 pointer-events: none 可能造成无障碍障碍。请始终确保禁用元素使用适当的 ARIA 属性正确标记,并且键盘用户无法意外聚焦它们。

使用场景

  • .pe-none - 在覆盖层、加载状态或临时禁用的元素上禁用交互
  • .pe-auto - 当父元素设置了 pe-none 时,在子元素上重新启用交互

CSS

Sass 工具类 API

指针事件工具类在我们的工具类 API 中于 scss/_utilities.scss 内声明。了解如何使用工具类 API。

"pointer-events": (
  property: pointer-events,
  class: pe,
  values: none auto,
),