Dropdowns下拉菜单
Toggle contextual overlays for displaying lists of links and more with the Bootstrap dropdown plugin.使用Bootstrap下拉插件切换上下文覆盖以显示链接列表等。
Overview概述
Dropdowns are toggleable, contextual overlays for displaying lists of links and more. 下拉列表是可切换的上下文覆盖,用于显示链接列表等。They’re made interactive with the included Bootstrap dropdown JavaScript plugin. 它们与附带的Bootstrap下拉JavaScript插件交互。They’re toggled by clicking, not by hovering; this is an intentional design decision.它们通过点击而不是悬停来切换;这是一个有意的设计决策。
Dropdowns are built on a third party library, Popper, which provides dynamic positioning and viewport detection. 下拉列表建立在第三方库Popper上,该库提供动态定位和视口检测。Be sure to include popper.min.js before Bootstrap’s JavaScript or use 确保在Bootstrap的JavaScript之前包含popper.min.js,或者使用包含popper的bootstrap.bundle.min.js
/ bootstrap.bundle.js
which contains Popper. bootstrap.bundle.min.js
/ bootstrap.bundle.js
。Popper isn’t used to position dropdowns in navbars though as dynamic positioning isn’t required.Popper不用于定位导航栏中的下拉列表,但不需要动态定位。
Accessibility可访问性
The WAI ARIA standard defines an actual WAI ARIA标准定义了一个实际role="menu"
widget, but this is specific to application-like menus which trigger actions or functions. role="menu"
小部件,但这是特定于触发动作或功能的类似应用程序的菜单。ARIA menus can only contain menu items, checkbox menu items, radio button menu items, radio button groups, and sub-menus.菜单只能包含菜单项、复选框菜单项、单选按钮菜单项、无线按钮组和子菜单。
Bootstrap’s dropdowns, on the other hand, are designed to be generic and applicable to a variety of situations and markup structures. 另一方面,Bootstrap的下拉列表设计为通用的,适用于各种情况和标记结构。For instance, it is possible to create dropdowns that contain additional inputs and form controls, such as search fields or login forms. 例如,可以创建包含其他输入和表单控件(如搜索字段或登录表单)的下拉列表。For this reason, Bootstrap does not expect (nor automatically add) any of the 因此,Bootstrap不期望(也不会自动添加)真正的aria菜单所需的任何role
and aria-
attributes required for true ARIA menus. role
和aria-
属性。Authors will have to include these more specific attributes themselves.作者必须包括这些更具体的属性。
However, Bootstrap does add built-in support for most standard keyboard menu interactions, such as the ability to move through individual 但是,Bootstrap确实为大多数标准键盘菜单交互添加了内置支持,例如使用光标键移动单个.dropdown-item
elements using the cursor keys and close the menu with the ESC key..dropdown-item
元素并使用ESC键关闭菜单的功能。
Examples例子
Wrap the dropdown’s toggle (your button or link) and the dropdown menu within 将下拉菜单的切换(按钮或链接)和下拉菜单包装在.dropdown
, or another element that declares position: relative;
. .dropdown
中,或另一个声明了position: relative;
的元素中。Dropdowns can be triggered from 下拉菜单可以从<a>
or <button>
elements to better fit your potential needs. <a>
或<button>
元素触发,以更好地满足您的潜在需求。The examples shown here use semantic 这里显示的示例在适当的情况下使用语义<ul>
elements where appropriate, but custom markup is supported.<ul>
元素,但支持自定义标记。
Single button单键
Any single 任何单个.btn
can be turned into a dropdown toggle with some markup changes. .btn
都可以通过一些标记更改转换为下拉切换。Here’s how you can put them to work with either 以下是您如何让他们与其中一个<button>
elements:<button>
元素一起工作:
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
And with 使用<a>
elements:<a>
元素:
<div class="dropdown">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown link
</a>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
The best part is you can do this with any button variant, too:最好的一点是,您也可以使用任何按钮变体:
<!-- Example single danger button -->
<div class="btn-group">
<button type="button" class="btn btn-danger dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Action
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
</div>
Split button拆分按钮
Similarly, create split button dropdowns with virtually the same markup as single button dropdowns, but with the addition of 类似地,使用与单个按钮下拉列表几乎相同的标记创建拆分按钮下拉列表,但添加了.dropdown-toggle-split
for proper spacing around the dropdown caret..dropdown-toggle-split
,以便在下拉插入符号周围保持适当的间距。
We use this extra class to reduce the horizontal 我们使用这个额外的类将插入符号两边的水平padding
on either side of the caret by 25% and remove the margin-left
that’s added for regular button dropdowns. padding
减少25%,并删除为常规按钮下拉添加的margin-left
。Those extra changes keep the caret centered in the split button and provide a more appropriately sized hit area next to the main button.这些额外的更改将插入符号保持在拆分按钮的中心,并在主按钮旁边提供更合适大小的命中区域。
<!-- Example split danger button -->
<div class="btn-group">
<button type="button" class="btn btn-danger">Action</button>
<button type="button" class="btn btn-danger dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
</div>
Sizing尺寸
Button dropdowns work with buttons of all sizes, including default and split dropdown buttons.按钮下拉菜单适用于所有大小的按钮,包括默认和拆分下拉按钮。
<!-- Large button groups (default and split) -->
<div class="btn-group">
<button class="btn btn-secondary btn-lg dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Large button
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary btn-lg" type="button">
Large split button
</button>
<button type="button" class="btn btn-lg btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Small button
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary btn-sm" type="button">
Small split button
</button>
<button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
...
</ul>
</div>
Dark dropdowns深色下拉列表
Opt into darker dropdowns to match a dark navbar or custom style by adding 通过在现有.dropdown-menu-dark
onto an existing .dropdown-menu
. .dropdown-menu
中添加.dropdown-menu-dark
,选择较暗的下拉菜单以匹配深色导航栏或自定义样式。No changes are required to the dropdown items.不需要对下拉项进行更改。
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton2" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown button
</button>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="dropdownMenuButton2">
<li><a class="dropdown-item active" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
</div>
And putting it to use in a navbar:并将其用于导航栏:
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNavDarkDropdown" aria-controls="navbarNavDarkDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDarkDropdown">
<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDarkDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu dropdown-menu-dark" aria-labelledby="navbarDarkDropdownMenuLink">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
</ul>
</div>
</div>
</nav>
Directions方向
RTL
Directions are mirrored when using Bootstrap in RTL, meaning 在RTL中使用Bootstrap时会镜像方向,这意味着.dropstart
will appear on the right side..dropstart
将出现在右侧。
Dropup
Trigger dropdown menus above elements by adding 通过将.dropup
to the parent element..dropup
添加到父元素,触发元素上方的下拉菜单。
<!-- Default dropup button -->
<div class="btn-group dropup">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropup
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
<!-- Split dropup button -->
<div class="btn-group dropup">
<button type="button" class="btn btn-secondary">
Split dropup
</button>
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
Dropright
Trigger dropdown menus at the right of the elements by adding 通过向父元素添加.dropend
to the parent element..dropend
,触发元素右侧的下拉菜单。
<!-- Default dropend button -->
<div class="btn-group dropend">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropright
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
<!-- Split dropend button -->
<div class="btn-group dropend">
<button type="button" class="btn btn-secondary">
Split dropend
</button>
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropright</span>
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
Dropleft
Trigger dropdown menus at the left of the elements by adding 通过向父元素添加.dropstart
to the parent element..dropstart
,触发元素左侧的下拉菜单。
<!-- Default dropstart button -->
<div class="btn-group dropstart">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropstart
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
<!-- Split dropstart button -->
<div class="btn-group">
<div class="btn-group dropstart" role="group">
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" data-bs-toggle="dropdown" aria-expanded="false">
<span class="visually-hidden">Toggle Dropstart</span>
</button>
<ul class="dropdown-menu">
<!-- Dropdown menu links -->
</ul>
</div>
<button type="button" class="btn btn-secondary">
Split dropstart
</button>
</div>
Menu items菜单项
You can use 您可以使用<a>
or <button>
elements as dropdown items.<a>
元素或<button>
元素作为下拉项。
<div class="dropdown">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu2">
<li><button class="dropdown-item" type="button">Action</button></li>
<li><button class="dropdown-item" type="button">Another action</button></li>
<li><button class="dropdown-item" type="button">Something else here</button></li>
</ul>
</div>
You can also create non-interactive dropdown items with 您还可以使用.dropdown-item-text
. .dropdown-item-text
创建非交互式下拉项。Feel free to style further with custom CSS or text utilities.您可以使用自定义CSS或文本实用程序进一步设置样式。
<ul class="dropdown-menu">
<li><span class="dropdown-item-text">Dropdown item text</span></li>
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
Active活动的
Add 向下拉列表中的项目添加.active
to items in the dropdown to style them as active. .active
以将其设置为活动项。To convey the active state to assistive technologies, use the 要将活动状态传递给辅助技术,请使用aria-current
attribute — using the page
value for the current page, or true
for the current item in a set.aria-current
属性-对当前页面使用page
值,或对集合中的当前项使用true
。
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Regular link</a></li>
<li><a class="dropdown-item active" href="#" aria-current="true">Active link</a></li>
<li><a class="dropdown-item" href="#">Another link</a></li>
</ul>
Disabled禁用的
Add 将下拉列表中对项目添加.disabled
to items in the dropdown to style them as disabled..disabled
,以将其设置为禁用项。
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Regular link</a></li>
<li><a class="dropdown-item disabled" href="#" tabindex="-1" aria-disabled="true">Disabled link</a></li>
<li><a class="dropdown-item" href="#">Another link</a></li>
</ul>
Menu alignment菜单对齐方式
By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. 默认情况下,下拉菜单将自动位于父菜单左侧的顶部100%的位置。You can change this with the directional 您可以使用方向.drop*
classes, but you can also control them with additional modifier classes..drop*
类来更改这一点,但也可以使用其他修饰符类来控制它们。
Add 将.dropdown-menu-end
to a .dropdown-menu
to right align the dropdown menu. .dropdown-menu-end
添加到.dropdown-menu
以右对齐下拉菜单。Directions are mirrored when using Bootstrap in RTL, meaning 在RTL中使用Bootstrap时,方向是镜像的,这意味着.dropdown-menu-end
will appear on the left side..dropdown-menu-end
将出现在左侧。
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Right-aligned menu example
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><button class="dropdown-item" type="button">Action</button></li>
<li><button class="dropdown-item" type="button">Another action</button></li>
<li><button class="dropdown-item" type="button">Something else here</button></li>
</ul>
</div>
Responsive alignment响应式对齐
If you want to use responsive alignment, disable dynamic positioning by adding the 如果您想使用响应对齐,则通过添加数data-bs-display="static"
attribute and use the responsive variation classes.data-bs-display="static"
属性并使用响应变体类来禁用动态定位。
To align right the dropdown menu with the given breakpoint or larger, add 要使下拉菜单与给定的断点或更大的断点对齐,请添加.dropdown-menu{-sm|-md|-lg|-xl|-xxl}-end
..dropdown-menu{-sm|-md|-lg|-xl|-xxl}-end
。
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
Left-aligned but right aligned when large screen
</button>
<ul class="dropdown-menu dropdown-menu-lg-end">
<li><button class="dropdown-item" type="button">Action</button></li>
<li><button class="dropdown-item" type="button">Another action</button></li>
<li><button class="dropdown-item" type="button">Something else here</button></li>
</ul>
</div>
To align left the dropdown menu with the given breakpoint or larger, add 要使下拉菜单左侧与给定的断点或更大的断点对齐,请添.dropdown-menu-end
and .dropdown-menu{-sm|-md|-lg|-xl|-xxl}-start
..dropdown-menu-end
和.dropdown-menu{-sm|-md|-lg|-xl|-xxl}-start
。
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
Right-aligned but left aligned when large screen
</button>
<ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
<li><button class="dropdown-item" type="button">Action</button></li>
<li><button class="dropdown-item" type="button">Another action</button></li>
<li><button class="dropdown-item" type="button">Something else here</button></li>
</ul>
</div>
Note that you don’t need to add a 注意,在导航栏中,您不需要给下拉按钮添加data-bs-display="static"
attribute to dropdown buttons in navbars, since Popper isn’t used in navbars.data-bs-display="static"
属性,因为导航栏中不使用Popper。
Alignment options对齐选项
Taking most of the options shown above, here’s a small kitchen sink demo of various dropdown alignment options in one place.利用上面显示的大多数选项,这里是一个小型厨房水槽演示,展示了一个地方的各种下拉对齐选项。
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Right-aligned menu
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
Left-aligned, right-aligned lg
</button>
<ul class="dropdown-menu dropdown-menu-lg-end">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false">
Right-aligned, left-aligned lg
</button>
<ul class="dropdown-menu dropdown-menu-end dropdown-menu-lg-start">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group dropstart">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropstart
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group dropend">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropend
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group dropup">
<button type="button" class="btn btn-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
Dropup
</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
Menu content菜单内容
Headers页眉
Add a header to label sections of actions in any dropdown menu.在任何下拉菜单中添加标题以标记操作部分。
<ul class="dropdown-menu">
<li><h6 class="dropdown-header">Dropdown header</h6></li>
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
</ul>
Dividers分隔器
Separate groups of related menu items with a divider.用分隔符分隔相关菜单项的组。
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
Text文本
Place any freeform text within a dropdown menu with text and use spacing utilities. 在带有文本的下拉菜单中放置任意自由形式的文本,并使用间距实用程序。Note that you’ll likely need additional sizing styles to constrain the menu width.请注意,您可能需要其他大小调整样式来约束菜单宽度。
<div class="dropdown-menu p-4 text-muted" style="max-width: 200px;">
<p>
Some example text that's free-flowing within the dropdown menu.
</p>
<p class="mb-0">
And this is more example text.
</p>
</div>
Forms表单
Put a form within a dropdown menu, or make it into a dropdown menu, and use margin or padding utilities to give it the negative space you require.将表单放入下拉菜单,或将其放入下拉菜单中,并使用边距或填充实用程序为其提供所需的负空间。
<div class="dropdown-menu">
<form class="px-4 py-3">
<div class="mb-3">
<label for="exampleDropdownFormEmail1" class="form-label">Email address</label>
<input type="email" class="form-control" id="exampleDropdownFormEmail1" placeholder="email@example.com">
</div>
<div class="mb-3">
<label for="exampleDropdownFormPassword1" class="form-label">Password</label>
<input type="password" class="form-control" id="exampleDropdownFormPassword1" placeholder="Password">
</div>
<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="dropdownCheck">
<label class="form-check-label" for="dropdownCheck">
Remember me
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">New around here? Sign up</a>
<a class="dropdown-item" href="#">Forgot password?</a>
</div>
<form class="dropdown-menu p-4">
<div class="mb-3">
<label for="exampleDropdownFormEmail2" class="form-label">Email address</label>
<input type="email" class="form-control" id="exampleDropdownFormEmail2" placeholder="email@example.com">
</div>
<div class="mb-3">
<label for="exampleDropdownFormPassword2" class="form-label">Password</label>
<input type="password" class="form-control" id="exampleDropdownFormPassword2" placeholder="Password">
</div>
<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" id="dropdownCheck2">
<label class="form-check-label" for="dropdownCheck2">
Remember me
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Sign in</button>
</form>
Dropdown options下拉选项
Use 使用data-bs-offset
or data-bs-reference
to change the location of the dropdown.data-bs-offset
或data-bs-reference
更改下拉列表的位置。
<div class="d-flex">
<div class="dropdown me-1">
<button type="button" class="btn btn-secondary dropdown-toggle" id="dropdownMenuOffset" data-bs-toggle="dropdown" aria-expanded="false" data-bs-offset="10,20">
Offset
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuOffset">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</div>
<div class="btn-group">
<button type="button" class="btn btn-secondary">Reference</button>
<button type="button" class="btn btn-secondary dropdown-toggle dropdown-toggle-split" id="dropdownMenuReference" data-bs-toggle="dropdown" aria-expanded="false" data-bs-reference="parent">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuReference">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="#">Separated link</a></li>
</ul>
</div>
</div>
Auto close behavior自动关闭行为
By default, the dropdown menu is closed when clicking inside or outside the dropdown menu. 默认情况下,单击下拉菜单内部或外部时,下拉菜单将关闭。You can use the 您可以使用autoClose
option to change this behavior of the dropdown.autoClose
选项更改下拉列表的此行为。
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="defaultDropdown" data-bs-toggle="dropdown" data-bs-auto-close="true" aria-expanded="false">
Default dropdown
</button>
<ul class="dropdown-menu" aria-labelledby="defaultDropdown">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuClickableOutside" data-bs-toggle="dropdown" data-bs-auto-close="inside" aria-expanded="false">
Clickable outside
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableOutside">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuClickableInside" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">
Clickable inside
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuClickableInside">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
<div class="btn-group">
<button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuClickable" data-bs-toggle="dropdown" data-bs-auto-close="false" aria-expanded="false">
Manual close
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuClickable">
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
<li><a class="dropdown-item" href="#">Menu item</a></li>
</ul>
</div>
Sass
Variables变量
Variables for all dropdowns:所有下拉列表的变量:
$dropdown-min-width: 10rem;
$dropdown-padding-x: 0;
$dropdown-padding-y: .5rem;
$dropdown-spacer: .125rem;
$dropdown-font-size: $font-size-base;
$dropdown-color: $body-color;
$dropdown-bg: $white;
$dropdown-border-color: rgba($black, .15);
$dropdown-border-radius: $border-radius;
$dropdown-border-width: $border-width;
$dropdown-inner-border-radius: subtract($dropdown-border-radius, $dropdown-border-width);
$dropdown-divider-bg: $dropdown-border-color;
$dropdown-divider-margin-y: $spacer * .5;
$dropdown-box-shadow: $box-shadow;
$dropdown-link-color: $gray-900;
$dropdown-link-hover-color: shade-color($gray-900, 10%);
$dropdown-link-hover-bg: $gray-200;
$dropdown-link-active-color: $component-active-color;
$dropdown-link-active-bg: $component-active-bg;
$dropdown-link-disabled-color: $gray-500;
$dropdown-item-padding-y: $spacer * .25;
$dropdown-item-padding-x: $spacer;
$dropdown-header-color: $gray-600;
$dropdown-header-padding: $dropdown-padding-y $dropdown-item-padding-x;
Variables for the dark dropdown:深色下拉菜单的变量:
$dropdown-dark-color: $gray-300;
$dropdown-dark-bg: $gray-800;
$dropdown-dark-border-color: $dropdown-border-color;
$dropdown-dark-divider-bg: $dropdown-divider-bg;
$dropdown-dark-box-shadow: null;
$dropdown-dark-link-color: $dropdown-dark-color;
$dropdown-dark-link-hover-color: $white;
$dropdown-dark-link-hover-bg: rgba($white, .15);
$dropdown-dark-link-active-color: $dropdown-link-active-color;
$dropdown-dark-link-active-bg: $dropdown-link-active-bg;
$dropdown-dark-link-disabled-color: $gray-500;
$dropdown-dark-header-color: $gray-500;
Variables for the CSS-based carets that indicate a dropdown’s interactivity:用于指示下拉列表交互性的基于CSS的插入符号的变量:
$caret-width: .3em;
$caret-vertical-align: $caret-width * .85;
$caret-spacing: $caret-width * .85;
Mixins
Mixins are used to generate the CSS-based carets and can be found in 混合符号用于生成基于CSS的插入符号,可以在scss/mixins/_caret.scss
.scss/mixins/_caret.scss
中找到。
@mixin caret-down {
border-top: $caret-width solid;
border-right: $caret-width solid transparent;
border-bottom: 0;
border-left: $caret-width solid transparent;
}
@mixin caret-up {
border-top: 0;
border-right: $caret-width solid transparent;
border-bottom: $caret-width solid;
border-left: $caret-width solid transparent;
}
@mixin caret-end {
border-top: $caret-width solid transparent;
border-right: 0;
border-bottom: $caret-width solid transparent;
border-left: $caret-width solid;
}
@mixin caret-start {
border-top: $caret-width solid transparent;
border-right: $caret-width solid;
border-bottom: $caret-width solid transparent;
}
@mixin caret($direction: down) {
@if $enable-caret {
&::after {
display: inline-block;
margin-left: $caret-spacing;
vertical-align: $caret-vertical-align;
content: "";
@if $direction == down {
@include caret-down();
} @else if $direction == up {
@include caret-up();
} @else if $direction == end {
@include caret-end();
}
}
@if $direction == start {
&::after {
display: none;
}
&::before {
display: inline-block;
margin-right: $caret-spacing;
vertical-align: $caret-vertical-align;
content: "";
@include caret-start();
}
}
&:empty::after {
margin-left: 0;
}
}
}
Usage用法
Via data attributes or JavaScript, the dropdown plugin toggles hidden content (dropdown menus) by toggling the 通过数据属性或JavaScript,下拉插件通过切换父.show
class on the parent .dropdown-menu
. .dropdown-menu
上的.show
类来切换隐藏内容(下拉菜单)。The data-bs-toggle="dropdown"
attribute is relied on for closing dropdown menus at an application level, so it’s a good idea to always use it.data-bs-toggle="dropdown"
属性用于在应用程序级别关闭下拉菜单,所以始终使用它是一个好主意。
mouseover
handlers to the immediate children of the <body>
element. mouseover
处理程序添加到<body>
元素的直接子级。mouseover
handlers are removed.mouseover
处理程序。
Via data attributes通过数据属性
Add 添加data-bs-toggle="dropdown"
to a link or button to toggle a dropdown.data-bs-toggle="dropdown"
指向链接或按钮以切换下拉列表。
<div class="dropdown">
<button id="dLabel" type="button" data-bs-toggle="dropdown" aria-expanded="false">
Dropdown trigger
</button>
<ul class="dropdown-menu" aria-labelledby="dLabel">
...
</ul>
</div>
Via JavaScript
Call the dropdowns via JavaScript:通过JavaScript调用下拉菜单:
var dropdownElementList = [].slice.call(document.querySelectorAll('.dropdown-toggle'))
var dropdownList = dropdownElementList.map(function (dropdownToggleEl) {
return new bootstrap.Dropdown(dropdownToggleEl)
})
data-bs-toggle="dropdown"
still required仍然需要
Regardless of whether you call your dropdown via JavaScript or instead use the data-api, 无论您是通过JavaScript调用下拉列表还是使用数据api,data-bs-toggle="dropdown"
is always required to be present on the dropdown’s trigger element.data-bs-toggle="dropdown"
始终需要出现在下拉列表的触发器元素上。
Options选项
Options can be passed via data attributes or JavaScript. 选项可以通过数据属性或JavaScript传递。For data attributes, append the option name to 对于数据属性,将选项名称附加到data-bs-
, as in data-bs-offset=""
. data-bs-
,如data-bs-offset=""
。Make sure to change the case type of the option name from camelCase to kebab-case when passing the options via data attributes. 通过数据属性传递选项时,确保将选项名称的大小写类型从camelCase更改为kebab case。For example, instead of using 例如,代替使用data-bs-autoClose="false"
, use data-bs-auto-close="false"
.data-bs-autoClose="false"
,使用data-bs-auto-close="false"
。
boundary |
string | element | 'clippingParents' |
'clippingParents' and can accept an HTMLElement reference (via JavaScript only). 'clippingParents' ,可以接受HTMLElement引用(仅通过JavaScript)。 |
reference |
string | element | object | 'toggle' |
'toggle' , 'parent' , an HTMLElement reference or an object providing getBoundingClientRect . 'toggle' 、'parent' 、HTMLElement引用或提供getBoundingClientRect 的对象的值。 |
display |
string | 'dynamic' |
static .static 禁用此功能。 |
offset |
array | string | function | [0, 2] |
|
autoClose |
boolean | string | true |
|
popperConfig |
null | object | function | null |
|
Using function with popperConfig
将函数与popperConfig
一起使用
popperConfig
var dropdown = new bootstrap.Dropdown(element, {
popperConfig: function (defaultBsPopperConfig) {
// var newPopperConfig = {...}
// use defaultBsPopperConfig if needed...
// return newPopperConfig
}
})
Methods方法
toggle |
|
show |
|
hide |
|
update |
|
dispose |
|
getInstance
|
bootstrap.Dropdown.getInstance(element)
|
getOrCreateInstance
|
bootstrap.Dropdown.getOrCreateInstance(element)
|
Events事件
All dropdown events are fired at the toggling element and then bubbled up. 所有下拉事件都在切换元素处触发,然后弹出。So you can also add event listeners on the 因此,您还可以在.dropdown-menu
’s parent element. hide.bs.dropdown
and hidden.bs.dropdown
events have a clickEvent
property (only when the original Event type is click
) that contains an Event Object for the click event..dropdown-menu
的父元素上添加事件侦听器。hide.bs.dropdown
和hidden.bs.dropdown
事件具有clickEvent
属性(仅当原始事件类型为click
时),该属性包含单击事件的事件对象。
show.bs.dropdown
|
|
shown.bs.dropdown
|
|
hide.bs.dropdown
|
|
hidden.bs.dropdown
|
|
var myDropdown = document.getElementById('myDropdown')
myDropdown.addEventListener('show.bs.dropdown', function () {
// do something...
})