菜单

切换上下文菜单和自定义叠加层,用于按钮、链接、表单和其他自定义内容的列表。

Requires JS
Layer: components
Viewport queries
1 dependency

概述

尽可能使用按钮来切换菜单。下面是一个使用 Bootstrap 按钮的示例:

HTML
<button class="btn-solid theme-primary" type="button" data-bs-toggle="menu" aria-expanded="false">
  Toggle menu
</button>
<div class="menu">
  <a class="menu-item" href="#">Menu item 1</a>
  <a class="menu-item" href="#">Menu item 2</a>
  <a class="menu-item" href="#">Menu item 3</a>
</div>

菜单是可切换的上下文叠加层,用于显示链接列表及更多内容。它们通过包含的 Bootstrap 菜单 JavaScript 插件实现交互。它们通过点击切换,而不是悬停;这是一个深思熟虑的设计决定。

菜单构建在第三方库 Floating UI 之上,它提供动态定位和视口检测。使用包含 Floating UI 的 bootstrap.bundle.min.js / bootstrap.bundle.js,或通过 import map单独引入 Floating UI。不过在导航栏中不使用 Floating UI 来定位菜单,因为不需要动态定位。

示例

虽然 <button> 是菜单切换的推荐控件,但有时你可能需要使用 <a> 元素。如果这样做,我们建议添加 role="button" 属性,以适当地向屏幕阅读器等辅助技术传达控件的用途。

使用按钮组创建拆分按钮菜单,点击次要按钮时显示菜单。

HTML
<a class="btn-solid theme-primary" href="#" role="button" data-bs-toggle="menu" aria-expanded="false">
  Toggle menu
</a>
<div class="menu">
  <a class="menu-item" href="#">Menu item 1</a>
  <a class="menu-item" href="#">Menu item 2</a>
  <a class="menu-item" href="#">Menu item 3</a>
</div>

<div class="btn-group">
  <button type="button" class="btn-solid theme-secondary">Split</button>
  <button type="button" class="btn-solid theme-secondary menu-toggle-split" data-bs-toggle="menu" aria-expanded="false">
    <svg class="bi" width="16" height="16" aria-hidden="true"><use href="#chevron-expand" /></svg>
    <span class="visually-hidden">Toggle menu</span>
  </button>
  <div class="menu">
    <a class="menu-item" href="#">Action</a>
    <a class="menu-item" href="#">Another action</a>
    <a class="menu-item" href="#">Something else here</a>
    <hr class="menu-divider">
    <a class="menu-item" href="#">Separated link</a>
  </div>
</div>

深色菜单

通过将 data-bs-theme="dark" 添加到切换元素或父元素上,选择深色菜单以匹配深色导航栏或自定义样式。

HTML
<div data-bs-theme="dark">
  <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" aria-expanded="false">
    Menu button
  </button>
  <div class="menu">
    <a class="menu-item active" href="#">Action</a>
    <a class="menu-item" href="#">Another action</a>
    <a class="menu-item" href="#">Something else here</a>
    <hr class="menu-divider">
    <a class="menu-item" href="#">Separated link</a>
  </div>
</div>

并在导航栏中使用它:

HTML
<nav class="navbar" data-bs-theme="dark">
  <div class="container-fluid">
    <a class="navbar-brand" href="#">Navbar</a>
    <ul class="navbar-nav">
      <li class="nav-item" data-bs-theme="dark">
        <button class="btn-solid theme-secondary" data-bs-toggle="menu" aria-expanded="false">
          Menu
        </button>
        <div class="menu">
          <a class="menu-item" href="#">Action</a>
          <a class="menu-item" href="#">Another action</a>
          <a class="menu-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

半透明菜单

使用 .menu-translucent 模糊并饱和菜单背景,以创建半透明效果。

HTML
<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" aria-expanded="false">
  Menu button
</button>
<div class="menu menu-translucent">
  <a class="menu-item active" href="#">Active menu item</a>
  <a class="menu-item" href="#">Menu item</a>
  <a class="menu-item" href="#">Menu item</a>
  <a class="menu-item" href="#">Menu item</a>
  <a class="menu-item" href="#">Menu item</a>
  <hr class="menu-divider">
  <a class="menu-item theme-danger" href="#">Danger menu item</a>
</div>

位置

在切换元素上使用 data-bs-placement 来控制菜单出现的位置。

  • 物理位置: top、bottom、left、right —— 无论文本方向如何,方向固定。
  • 逻辑位置: start、end —— 根据 RTL 自动翻转。在 LTR 模式下,start 变为 left,end 变为 right。在 RTL 模式下,它们互换。

所有位置都支持 -start 和 -end 对齐修饰符(例如 bottom-start、end-end)。

HTML
<button class="btn-solid theme-primary" type="button" data-bs-toggle="menu" data-bs-placement="bottom-start" aria-expanded="false">
  Toggle menu
</button>
<div class="menu">
  <button class="menu-item" type="button">Action</button>
  <button class="menu-item" type="button">Another action</button>
  <button class="menu-item" type="button">Something else here</button>
</div>

响应式

使用响应式前缀在不同断点更改位置。语法为 breakpoint:placement,其中 breakpoint 为 sm、md、lg、xl 或 2xl 之一。可以在单个属性中组合多个断点,用空格分隔。

例如,data-bs-placement="bottom-start md:bottom-end lg:end-start" 将:

  • 在小屏幕上将菜单显示在 bottom-start(默认)
  • 在 md 断点切换到 bottom-end
  • 在 lg 断点切换到 right-start
HTML
<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-placement="bottom-start md:bottom-end" aria-expanded="false">
  Bottom start → md:bottom-end
</button>
<div class="menu">
  <a class="menu-item" href="#">Action</a>
  <a class="menu-item" href="#">Another action</a>
</div>

<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-placement="bottom lg:right" aria-expanded="false">
  Bottom → lg:right
</button>
<div class="menu">
  <a class="menu-item" href="#">Action</a>
  <a class="menu-item" href="#">Another action</a>
</div>

<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-placement="top-end md:right-start xl:bottom-start" aria-expanded="false">
  Multi-breakpoint
</button>
<div class="menu">
  <a class="menu-item" href="#">Action</a>
  <a class="menu-item" href="#">Another action</a>
</div>

调整浏览器窗口大小,以查看位置在不同断点的变化。

菜单项

你可以使用 <a> 或 <button> 元素作为菜单项。

在我们的文档示例中,我们使用工具类来显示菜单,但你自己的使用中并不需要它们。

HTML
<div class="menu show position-static">
  <button class="menu-item" type="button">Action</button>
  <button class="menu-item" type="button">Another action</button>
  <button class="menu-item" type="button">Something else here</button>
</div>

你还可以使用 .menu-text 创建非交互式菜单项。随意使用自定义 CSS 或文本工具类进一步设置样式。

HTML
<div class="menu show position-static">
  <span class="menu-text">Menu item text</span>
  <a class="menu-item" href="#">Action</a>
  <a class="menu-item" href="#">Another action</a>
  <a class="menu-item" href="#">Something else here</a>
</div>

激活

为菜单中的项目添加 .active,将它们样式化为激活的选择。要向辅助技术传达激活状态,请使用 aria-current 属性——对当前页面使用 page 值,或对一组中的当前项使用 true。

HTML
<div class="menu show position-static">
  <a class="menu-item" href="#">Regular link</a>
  <a class="menu-item active" href="#" aria-current="true">Active link</a>
  <a class="menu-item" href="#">Another link</a>
</div>

禁用

为菜单中的项目添加 .disabled,将它们样式化为禁用状态。

HTML
<div class="menu show position-static">
  <a class="menu-item" href="#">Regular link</a>
  <a class="menu-item disabled" aria-disabled="true">Disabled link</a>
  <a class="menu-item" href="#">Another link</a>
</div>

变体

你可以使用 theme- 前缀为菜单项应用主题颜色。

HTML
<div class="menu show position-static">
  <a class="menu-item theme-primary" href="#">Primary link</a>
  <a class="menu-item theme-secondary" href="#">Secondary link</a>
  <a class="menu-item theme-success" href="#">Success link</a>
  <a class="menu-item theme-danger" href="#">Danger link</a>
</div>
<div class="menu show position-static">
  <a class="menu-item active theme-primary" href="#">Primary link</a>
  <a class="menu-item active theme-secondary" href="#">Secondary link</a>
  <a class="menu-item active theme-success" href="#">Success link</a>
  <a class="menu-item active theme-danger" href="#">Danger link</a>
</div>

图标

直接将 .menu-item-icon 添加到 <svg> 上,为菜单项添加图标槽。

HTML
<div class="menu show position-static">
  <button class="menu-item" type="button">
    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="menu-item-icon fg-3" viewBox="0 0 16 16">
      <path fill-rule="evenodd" d="M4 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2zm2-1a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 5a1 1 0 0 0-1 1v8a1 1 0 0 0 1 1h8a1 1 0 0 0 1-1v-1h1v1a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h1v1z"/>
    </svg>
    Copy
  </button>
  <button class="menu-item" type="button">
    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="menu-item-icon fg-3" viewBox="0 0 16 16">
      <path d="M3.5 3.5c-.614-.884-.074-1.962.858-2.5L8 7.226 11.642 1c.932.538 1.472 1.616.858 2.5L8.81 8.61l1.556 2.661a2.5 2.5 0 1 1-.794.637L8 9.73l-1.572 2.177a2.5 2.5 0 1 1-.794-.637L7.19 8.61zm2.5 10a1.5 1.5 0 1 0-3 0 1.5 1.5 0 0 0 3 0m7 0a1.5 1.5 0 1 0-3 0 1.5 1.5 0 0 0 3 0"/>
    </svg>
    Cut
  </button>
  <hr class="menu-divider">
  <button class="menu-item theme-danger" type="button">
    <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="menu-item-icon" viewBox="0 0 16 16">
      <path d="M6.5 1h3a.5.5 0 0 1 .5.5v1H6v-1a.5.5 0 0 1 .5-.5M11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3A1.5 1.5 0 0 0 5 1.5v1H1.5a.5.5 0 0 0 0 1h.538l.853 10.66A2 2 0 0 0 4.885 16h6.23a2 2 0 0 0 1.994-1.84l.853-10.66h.538a.5.5 0 0 0 0-1zm1.958 1-.846 10.58a1 1 0 0 1-.997.92h-6.23a1 1 0 0 1-.997-.92L3.042 3.5zm-7.487 1a.5.5 0 0 1 .528.47l.5 8.5a.5.5 0 0 1-.998.06L5 5.03a.5.5 0 0 1 .47-.53Zm5.058 0a.5.5 0 0 1 .47.53l-.5 8.5a.5.5 0 1 1-.998-.06l.5-8.5a.5.5 0 0 1 .528-.47M8 4.5a.5.5 0 0 1 .5.5v8.5a.5.5 0 0 1-1 0V5a.5.5 0 0 1 .5-.5"/>
    </svg>
    Delete
  </button>
</div>

描述

使用 .menu-item-content 实现 Flex 列布局,使用 .menu-item-description 在主标签下方显示次要文本。

HTML
<div class="menu show position-static">
  <button class="menu-item" type="button">
    <span class="menu-item-content">
      Publish now
      <small class="menu-item-description">Make this page visible immediately</small>
    </span>
  </button>
  <button class="menu-item" type="button">
    <span class="menu-item-content">
      Schedule
      <small class="menu-item-description">Set a future publish date</small>
    </span>
  </button>
</div>

选中

为 .menu-item 添加 .selected 以指示当前选择。

HTML
<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" aria-expanded="false">Select status</button>
<div class="menu">
  <button class="menu-item selected" type="button">
    <div class="menu-item-content">
      选中的菜单项
      <small class="menu-item-description">Currently visible to users</small>
    </div>
    <svg class="menu-item-check" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 16 16"><path d="m2 7 4 5 8-8"/></svg>
  </button>
  <button class="menu-item" type="button">
    <div class="menu-item-content">
      默认菜单项
      <small class="menu-item-description">Not yet published</small>
    </div>
    <svg class="menu-item-check" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 16 16"><path d="m2 7 4 5 8-8"/></svg>
  </button>
</div>

<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" aria-expanded="false">Select status</button>
<div class="menu">
  <button class="menu-item" type="button">
    默认菜单项
    <svg class="menu-item-check" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 16 16"><path d="m2 7 4 5 8-8"/></svg>
  </button>
  <button class="menu-item selected" type="button">
    选中的菜单项
    <svg class="menu-item-check" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 16 16"><path d="m2 7 4 5 8-8"/></svg>
  </button>
  <button class="menu-item" type="button">
    默认菜单项
    <svg class="menu-item-check" xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 16 16"><path d="m2 7 4 5 8-8"/></svg>
  </button>
</div>

菜单内容

页眉

为任何菜单中的操作区块添加页眉标签。

HTML
<div class="menu show position-static">
  <a class="menu-item" href="#">Action</a>
  <a class="menu-item" href="#">Action</a>
  <h6 class="menu-header">Menu header</h6>
  <a class="menu-item" href="#">Another action</a>
</div>

分隔符

使用分隔符分隔相关菜单项的组。

HTML
<div class="menu show position-static">
  <a class="menu-item" href="#">Action</a>
  <a class="menu-item" href="#">Another action</a>
  <a class="menu-item" href="#">Something else here</a>
  <hr class="menu-divider">
  <a class="menu-item" href="#">Separated link</a>
</div>

文本

使用文本在菜单中放置任何自由格式的文本,并使用 margin 和 padding工具类。请注意,你很可能需要额外的尺寸样式来约束菜单宽度。

HTML
<div class="menu show position-static p-3 fg-2"
     style="--bs-menu-min-width: 240px;">
  <p>
    一些在菜单中自由流动的示例文本。
  </p>
  <p class="mb-0">
    而这是更多示例文本。
  </p>
</div>

表单

在菜单中放置一个表单,或者将其变为菜单,并使用 margin 和 padding工具类为其提供所需的负空间。

HTML
<div class="menu show position-static"
     style="--bs-menu-min-width: 300px;">
  <form class="d-flex flex-column gap-3 p-3">
    <div>
      <label for="exampleMenuFormEmail1" class="form-label">Email address</label>
      <input type="email" class="form-control" id="exampleMenuFormEmail1" placeholder="email@example.com">
    </div>
    <div>
      <label for="exampleMenuFormPassword1" class="form-label">Password</label>
      <input type="password" class="form-control" id="exampleMenuFormPassword1" placeholder="Password">
    </div>
    <div class="form-field">
      <input type="checkbox" id="checkLabel" class="check" />
      <label for="checkLabel">Example new checkbox</label>
    </div>
    <div class="vstack gap-2">
      <button type="submit" class="btn-solid theme-primary">Sign in</button>
      <a class="theme-secondary" href="#">New around here? Sign up</a>
      <a class="theme-secondary" href="#">Forgot password?</a>
    </div>
  </form>
</div>

可滚动菜单

动态菜单会换行长标签,并在可用空间太小时滚动。要进一步限制高度,请使用下面的选项。

分配 --bs-menu-max-height 和 --bs-menu-overflow-y 自定义属性,以约束菜单高度并启用滚动。

HTML
<div class="menu show position-static" style="--bs-menu-max-height: 145px; --bs-menu-overflow-y: auto;">
  <button class="menu-item" type="button">Item 1</button>
  <button class="menu-item" type="button">Item 2</button>
  <button class="menu-item" type="button">Item 3</button>
  <button class="menu-item" type="button">Item 4</button>
  <button class="menu-item" type="button">Item 5</button>
  <button class="menu-item" type="button">Item 6</button>
  <button class="menu-item" type="button">Item 7</button>
  <button class="menu-item" type="button">Item 8</button>
</div>

你还可以使用 .menu-scrollable 类使菜单可滚动。这会假定一个通常安全的 80dvh 最大高度,并为你设置溢出。

子菜单

使用 .submenu 包装类创建嵌套菜单。子菜单支持悬停、点击和键盘导航。在 576px 以下或父菜单滚动时,选择子菜单以代替父菜单显示。选择返回以返回父菜单。

将 .menu-item 触发器和嵌套的 .menu 包裹在 .submenu 元素中。

HTML
<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" aria-expanded="false">
  Submenus
</button>
<div class="menu">
  <div class="submenu">
    <button class="menu-item" type="button">File</button>
    <div class="menu">
      <a class="menu-item" href="#">New</a>
      <a class="menu-item" href="#">Open</a>
      <a class="menu-item" href="#">Save</a>
    </div>
  </div>
  <div class="submenu">
    <button class="menu-item" type="button">Edit</button>
    <div class="menu">
      <a class="menu-item" href="#">Cut</a>
      <a class="menu-item" href="#">Copy</a>
      <a class="menu-item" href="#">Paste</a>
    </div>
  </div>
  <div class="submenu">
    <button class="menu-item" type="button">View</button>
    <div class="menu">
      <a class="menu-item" href="#">Zoom In</a>
      <a class="menu-item" href="#">Zoom Out</a>
    </div>
  </div>
  <hr class="menu-divider">
  <a class="menu-item" href="#">Preferences</a>
</div>

嵌套

子菜单可以有多个层级。在 576px 及以上,不滚动的菜单会向侧面展开每个层级。子菜单可以翻转以适应视口。

HTML
<button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" aria-expanded="false">
  Multi-level menu
</button>
<div class="menu">
  <a class="menu-item" href="#">Level 1 action</a>
  <div class="submenu">
    <button class="menu-item" type="button">Level 1 submenu</button>
    <div class="menu">
      <a class="menu-item" href="#">Level 2 action</a>
      <div class="submenu">
        <button class="menu-item" type="button">Level 2 submenu</button>
        <div class="menu">
          <a class="menu-item" href="#">Level 3 action A</a>
          <a class="menu-item" href="#">Level 3 action B</a>
        </div>
      </div>
      <a class="menu-item" href="#">Another level 2</a>
    </div>
  </div>
  <a class="menu-item" href="#">Another level 1</a>
</div>

菜单选项

使用 data-bs-offset 或 data-bs-reference 更改菜单的位置。Offset 是与触发元素的像素距离。Reference 是从其引用菜单的元素。

HTML
<div class="d-flex">
  <div class="me-1">
    <button type="button" class="btn-solid theme-secondary" data-bs-toggle="menu" aria-expanded="false" data-bs-offset="10,20">
      Offset
    </button>
    <div class="menu">
      <a class="menu-item" href="#">Action</a>
      <a class="menu-item" href="#">Another action</a>
      <a class="menu-item" href="#">Something else here</a>
    </div>
  </div>
  <div class="btn-group">
    <button type="button" class="btn-solid theme-secondary">Reference</button>
    <button type="button" class="btn-solid theme-secondary menu-toggle-split" data-bs-toggle="menu" aria-expanded="false" data-bs-reference="parent">
      <svg class="bi" width="16" height="16" aria-hidden="true"><use href="#chevron-expand" /></svg>
      <span class="visually-hidden">Toggle Menu</span>
    </button>
    <div class="menu">
      <a class="menu-item" href="#">Action</a>
      <a class="menu-item" href="#">Another action</a>
      <a class="menu-item" href="#">Something else here</a>
      <hr class="menu-divider">
      <a class="menu-item" href="#">Separated link</a>
    </div>
  </div>
</div>

自动关闭行为

默认情况下,点击菜单内部或外部时会关闭菜单。你可以使用 autoClose 选项更改菜单的这种行为。

无论 autoClose 设置如何,当焦点位于菜单内(其切换按钮或菜单项)时按 Esc 会关闭菜单并将焦点返回给切换按钮。

HTML
<div class="btn-group">
  <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="true" aria-expanded="false">
    Default menu
  </button>
  <div class="menu">
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
  </div>
</div>

<div class="btn-group">
  <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="inside" aria-expanded="false">
    Clickable inside
  </button>
  <div class="menu">
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
  </div>
</div>

<div class="btn-group">
  <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="outside" aria-expanded="false">
    Clickable outside
  </button>
  <div class="menu">
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
  </div>
</div>

<div class="btn-group">
  <button class="btn-solid theme-secondary" type="button" data-bs-toggle="menu" data-bs-auto-close="false" aria-expanded="false">
    Manual close
  </button>
  <div class="menu">
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
    <a class="menu-item" href="#">Menu item</a>
  </div>
</div>

无障碍

WAI ARIA 标准定义了一个实际的 role="menu" 小部件,但这特定于触发操作或功能的类似应用的菜单。ARIA 菜单只能包含菜单项、复选框菜单项、单选按钮菜单项、单选按钮组和子菜单。

另一方面,Bootstrap 的菜单设计为通用且适用于各种情况和标记结构。例如,可以创建包含额外输入和表单控件(如搜索字段或登录表单)的菜单。因此,Bootstrap 不期望(也不会自动添加)真正的 ARIA 菜单所需的任何 role 和 aria- 属性。作者必须自行包含这些更具体的属性。

子菜单触发器是一个例外。Bootstrap 会为每个包裹嵌套 .menu 的 .submenu > .menu-item 添加 aria-haspopup="true",并在子菜单打开和关闭时同步其上的 aria-expanded。仅当子菜单必须初始打开时,才自行设置 aria-expanded。

键盘导航

菜单包含对菜单项导航的内置键盘支持。原生表单控件保留其自己的方向键行为。键盘焦点使用独特的背景,你可以通过 --bs-menu-item-focus-bg 和 --bs-menu-item-focus-color 自定义。

按键操作
↓ / ↑导航到下一个/上一个菜单项
→进入子菜单(或在 RTL 中为 ←)
←退出子菜单并返回父菜单(或在 RTL 中为 →)
Enter / Space激活聚焦的项或打开子菜单
Esc关闭当前子菜单或菜单,并将焦点返回给其触发器
Home / End跳转到第一个/最后一个菜单项
Tab / Shift+Tab按顺序在控件间移动,包括当菜单使用 container 时。默认的 autoClose 会在焦点离开时关闭菜单。

CSS

变量

Menu use local CSS variables on .menu for real-time customization. Values for the CSS variables are generated from Sass maps unique to each component and applied to the aforementioned class.

$menu-tokens: defaults(
  (
    --menu-zindex: var(--z-menu),
    --menu-gap: .125rem,
    --menu-min-width: 10rem,
    --menu-padding-x: .25rem,
    --menu-padding-y: .25rem,
    --menu-spacer: .125rem,
    --menu-font-size: var(--font-size-sm),
    --menu-color: var(--fg-body),
    --menu-bg: var(--bg-body),
    // --menu-border-color: var(--border-color-translucent),
    // --menu-border-radius: var(--radius-7),
    // --menu-border-width: var(--border-width),
    --menu-box-shadow: var(--box-shadow-lg),
    // --menu-max-height: none,
    --menu-divider-bg: var(--border-color-translucent),
    --menu-divider-margin-y: .125rem,
    --menu-divider-margin-x: .25rem,
    --menu-item-color: var(--menu-color, var(--fg-body)),
    --menu-item-hover-color: var(--menu-color, var(--fg-body)),
    --menu-item-hover-bg: var(--bg-1),
    --menu-item-focus-color: light-dark(var(--white), var(--gray-900)),
    --menu-item-focus-bg: light-dark(var(--gray-700), var(--gray-200)),
    --menu-item-active-color: var(--primary-contrast),
    --menu-item-active-bg: var(--primary-bg),
    --menu-item-disabled-color: var(--fg-3),
    --menu-item-gap: .5rem,
    --menu-item-padding-x: .75rem,
    --menu-item-padding-y: .25rem,
    --menu-item-border-radius: var(--radius-5),
    --menu-icon-size: 1rem,
    --menu-description-font-size: var(--font-size-xs),
    --menu-check-color: currentcolor,
    --menu-header-color: var(--fg-3),
    --menu-header-padding-x: .75rem,
    --menu-header-padding-y: .25rem,
    --menu-transition-property: "opacity, transform, display",
    --menu-transition-duration: .15s,
    --menu-transition-timing: var(--transition-timing-overlay),
  ),
  $menu-tokens
);

菜单项包含至少一个未在 .menu 上设置的变量。这允许你提供新值,而 Bootstrap 默认回退到一个回退值。

  • --bs-menu-item-border-radius

用法

通过数据属性或 JavaScript,菜单插件通过在 .menu 上切换 .show 类来切换隐藏内容(菜单)。data-bs-toggle="menu" 属性在应用级别用于关闭菜单,因此始终使用它是好主意。

在支持触摸的设备上,打开菜单会为 <body> 元素的直接子元素添加空的 mouseover 处理器。这个虽然有些丑陋的技巧是必要的,用于绕过 iOS 事件委托的一个怪癖,否则菜单外任何地方的轻触都将无法触发关闭菜单的代码。一旦菜单关闭,这些额外的空 mouseover 处理器就会被移除。

通过数据属性

为链接或按钮添加 data-bs-toggle="menu" 以切换菜单。

属性描述
data-bs-toggle="menu"在触发器上初始化菜单并连接全局关闭行为。
HTML
          <button type="button" data-bs-toggle="menu" aria-expanded="false">
  Menu trigger
</button>
<div class="menu">
  ...
</div>
        

通过 JavaScript

无论你是通过 JavaScript 调用菜单还是使用 data-api,菜单的触发元素上都必须有 data-bs-toggle="menu"。

通过 JavaScript 调用菜单:

JavaScript
          const menuElementList = document.querySelectorAll('[data-bs-toggle="menu"]')
const menuList = [...menuElementList].map(menuToggleEl => new bootstrap.Menu(menuToggleEl))
        

要以编程方式打开、关闭或切换菜单,请从触发元素(带有 data-bs-toggle="menu" 的元素)获取其实例,并调用相应的方法:

JavaScript
          const menuToggleEl = document.querySelector('#myMenuToggle')
const menu = bootstrap.Menu.getOrCreateInstance(menuToggleEl)

menu.show()
menu.hide()
menu.toggle()
        

依赖

The menu plugin requires the following JavaScript files if you’re building Bootstrap’s JS from source:

FileDescription
js/src/menu.tsMain menu component
js/src/base-component.tsBase component class
js/src/dom/data.tsElement data store
js/src/dom/event-handler.tsEvent handling utilities
js/src/dom/manipulator.tsData attribute manipulation
js/src/dom/selector-engine.tsDOM selector utilities
js/src/util/config.tsConfiguration base class
js/src/util/floating-ui.tsResponsive placement utilities
js/src/util/index.tsCore utility functions
@floating-ui/domThird-party positioning library

选项

As options can be passed via data attributes or JavaScript, you can append an option name to data-bs-, as in data-bs-animation="{value}". Make sure to change the case type of the option name from “camelCase” to “kebab-case” when passing the options via data attributes. For example, use data-bs-custom-class="beautifier" instead of data-bs-customClass="beautifier".

All components support a reserved data attribute data-bs-config that can house simple component configuration as a JSON string. When an element has data-bs-config='{"delay":0, "title":123}' and data-bs-title="456" attributes, the final title value will be 456 and the separate data attributes will override values given on data-bs-config. In addition, existing data attributes are able to house JSON values like data-bs-delay='{"show":0,"hide":150}'.

The final configuration object is the merged result of data-bs-config, data-bs-, and js object where the latest given key-value overrides the others.

名称类型默认值描述
autoCloseboolean, stringtrue控制哪些点击会关闭菜单。
  • true 在内部或外部点击时关闭。
  • false 禁用自动关闭。
  • 'inside' 在内部点击时关闭。
  • 'outside' 在外部点击时关闭。
所有值都允许用户通过切换按钮关闭菜单,或当焦点在菜单内时按 Esc。
boundarystring, element'clippingParents'Floating UI 用于翻转、移动和调整菜单尺寸的边界。通过 JavaScript 传递一个元素以使用其边界。有关更多信息,请参阅 Floating UI 的 overflow 选项。
containerstring, element, booleanfalse显示时将菜单附加到特定元素。使用 'body' 或 true 附加到文档 body,这有助于逃离具有 overflow: hidden 的容器。隐藏时菜单会移回其原始位置。
displaystring'dynamic'默认情况下,我们使用 Floating UI 进行动态定位。使用 static 禁用。
offsetarray, string, function[0, 2]菜单相对于其目标的偏移量。你可以在数据属性中传递逗号分隔值的字符串,例如:data-bs-offset="10,20"。当使用函数确定偏移量时,它会以包含 placement、reference 和 floating rects 的对象作为第一个参数被调用。触发元素的 DOM 节点作为第二个参数传入。该函数必须返回一个包含两个数字的数组:[skidding, distance]。有关更多信息,请参阅 Floating UI 的 offset 文档。
floatingConfignull, object, functionnull要更改 Bootstrap 的默认 Floating UI 配置,请参阅 Floating UI 的配置。当使用函数创建 Floating UI 配置时,它会以包含 Bootstrap 默认 Floating UI 配置的对象被调用。这有助于你使用默认配置并将其与你自己的配置合并。该函数必须返回 Floating UI 的配置对象。
menunull, elementnull要使用的菜单元素。默认情况下,我们在切换按钮之后或之前查找 .menu,然后在其包装器内查找。当该搜索找到错误的元素,或菜单位于包装器之外时,设置 menu。
placementstring'bottom-start'菜单的位置。物理位置:'top'、'bottom'、'left'、'right'。逻辑位置(RTL 感知):'start'、'end'。都支持对齐修饰符:-start、-end。支持响应式前缀,如 'bottom-start md:end'。
referencestring, element, object'toggle'菜单的引用元素。接受 'toggle'、'parent'、HTMLElement 引用或提供 getBoundingClientRect 的对象。有关更多信息,请参阅 Floating UI 的虚拟元素文档。
strategystring'absolute'菜单的定位策略。使用 'absolute' 进行默认定位,或使用 'fixed' 逃离具有 overflow: hidden 的容器。有关更多信息,请参阅 Floating UI 的 strategy 文档。
submenuTriggerstring'both'子菜单的触发方式。使用 'click' 仅点击,'hover' 仅悬停,或 'both' 点击和悬停。堆叠子菜单在所有三个选项中都在点击时打开。
submenuDelaynumber100鼠标离开时关闭子菜单前的延迟(毫秒)。为朝子菜单的对角鼠标移动提供宽限期。

使用 floatingConfig 的函数

JavaScript
          const menu = new bootstrap.Menu(element, {
  floatingConfig(defaultBsFloatingConfig) {
    // const newFloatingConfig = {...}
    // use defaultBsFloatingConfig if needed...
    // return newFloatingConfig
  }
})
        

方法

方法描述
dispose销毁元素的菜单。(移除 DOM 元素上存储的数据)
getInstance静态方法,允许你获取与 DOM 元素关联的菜单实例,你可以这样使用:bootstrap.Menu.getInstance(element)。
getOrCreateInstance静态方法,返回与 DOM 元素关联的菜单实例,如果尚未初始化则创建一个新实例。你可以这样使用:bootstrap.Menu.getOrCreateInstance(element)。
hide隐藏给定导航栏或标签页导航的菜单。
show显示给定导航栏或标签页导航的菜单。
toggle切换给定导航栏或标签页导航的菜单。
update更新元素菜单的位置。

事件

所有菜单事件都在切换元素上触发,然后向上冒泡。因此你也可以在 .menu 的父元素上添加事件监听器。hide.bs.menu 和 hidden.bs.menu 事件有一个 clickEvent 属性(仅当原始 Event 类型为 click 时),其中包含点击事件的 Event 对象。

事件类型描述
hide.bs.menu当调用 hide 实例方法时立即触发。
hidden.bs.menu当菜单已完成对用户的隐藏且 CSS 过渡完成时触发。
show.bs.menu当调用 show 实例方法时立即触发。
shown.bs.menu当菜单已对用户可见且 CSS 过渡完成时触发。
JavaScript
          const myMenu = document.getElementById('myMenu')
myMenu.addEventListener('show.bs.menu', event => {
  // do something...
})