下拉菜单

下拉菜单可以在按钮被点击时打开一个菜单或任何其他元素。

类名
类型
dropdown
组件
下拉菜单容器
dropdown-content
部分
内容部分
dropdown-start
位置
水平对齐到按钮的起始位置[默认]
dropdown-center
位置
水平对齐到按钮的中心位置
dropdown-end
位置
水平对齐到按钮的末尾位置
dropdown-top
位置
从顶部打开
dropdown-bottom
位置
从底部打开[默认]
dropdown-left
位置
从左侧打开
dropdown-right
位置
从右侧打开
dropdown-hover
修饰符
鼠标悬停时也打开
dropdown-open
修饰符
强制打开

有 3 种方法可以使用下拉菜单

  1. details 和 summary 元素
  2. popover API 和锚点定位 new
  3. CSS 焦点

方法 1. details 和 summary

details 和 summary 是原生的 HTML 元素,可以用来创建下拉菜单。
使用 details 和 summary 的下拉菜单在摘要被点击时打开和关闭内容。你也可以通过使用 JS 添加/移除open属性

结构
  Container for the button + content

              │    button to toggle the visibility of the content
              │                        │
<details>   ──╯                        │
  <summary>open or close</summary>   ──╯
  Content
</details>

方法 2. popover API 和锚点定位new

Popover API 是创建下拉菜单的新的 HTML 标准。它在顶层打开下拉菜单,这意味着不需要 z-index 管理,也没有溢出问题。
锚点定位是新的 CSS 标准,用于相对于锚点定位元素。

结构
<button
  style="anchor-name:--anchor-1"  ────────────────╮
  popovertarget="popover-1"   ───────╮            │
>Button</button>                Unique ID         │
                                     │            │
<div popover                         │      Positions this dropdown relative to this button
  id="popover-1"   ──────────────────╯            │
  style="position-anchor:--anchor-1"  ────────────╯
>Content</div>

popovertarget是 popover 内容的唯一 ID。
anchor-name/position-anchor是锚点的唯一名称。

CSS 锚点定位是一个新标准,但尚未在 Firefox 和 Safari 中得到支持 (caniuse.com).
在这些浏览器中,下拉菜单将像模态框一样居中显示。
还有这个 polyfill可能会有帮助。

方法 3. CSS 焦点

当按钮获得焦点时,内容会显示。

结构
                                                    Focusable button

<div>                                                       │
  <div tabindex="0" role="button">Click to open</div>  ─────╯
  <div tabindex="0">Content</div>   ───────╮ 
</div>                                     │
                           Content shown when button is focused

为什么是 div?
Safari 有一个CSS 错误自 2008 年以来,阻止按钮元素获得焦点的 CSS 错误,所以我们使用 divtabindex="0"作为一种解决方法。 这种方法通过添加role="button"可以在所有浏览器中保持一致的功能。

当焦点丢失时,此下拉菜单会关闭。避免在其中放置会从下拉菜单内容中移除焦点的元素。例如一个<dialog>下拉菜单内的元素。

位置

修饰符

强制打开

<div class="$$dropdown $$dropdown-open">
  <div tabindex="0" role="button" class="$$btn m-1">Button</div>
  <ul tabindex="0" class="$$dropdown-content $$menu bg-base-100 rounded-box z-1 w-52 p-2 shadow-sm">
    <li><a>Item 1</a></li>
    <li><a>Item 2</a></li>
  </ul>
</div>
<div class="$$dropdown $$dropdown-open">
  <div tabindex="0" role="button" class="$$btn m-1">Button</div>
  <ul tabindex="0" class="$$dropdown-content $$menu bg-base-100 rounded-box z-1 w-52 p-2 shadow-sm">
    <li><a>Item 1</a></li>
    <li><a>Item 2</a></li>
  </ul>
</div>

更多示例

卡片作为下拉菜单

<div class="$$dropdown">
  <div tabindex="0" role="button" class="$$btn m-1">Click</div>
  <div
    tabindex="0"
    class="$$dropdown-content $$card $$card-sm bg-base-100 z-1 w-64 shadow-md">
    <div class="$$card-body">
      <p>This is a card. You can use any element as a dropdown.</p>
    </div>
  </div>
</div>
<div class="$$dropdown">
  <div tabindex="0" role="button" class="$$btn m-1">Click</div>
  <div
    tabindex="0"
    class="$$dropdown-content $$card $$card-sm bg-base-100 z-1 w-64 shadow-md">
    <div class="$$card-body">
      <p>This is a card. You can use any element as a dropdown.</p>
    </div>
  </div>
</div>

辅助下拉菜单

一段普通文本和一个辅助下拉菜单
A normal text and a helper dropdown
<div class="$$dropdown $$dropdown-end">
  <div tabindex="0" role="button" class="$$btn $$btn-circle $$btn-ghost $$btn-xs text-info">
    <svg
      tabindex="0"
      xmlns="http://www.w3.org/2000/svg"
      fill="none"
      viewBox="0 0 24 24"
      class="h-4 w-4 stroke-current">
      <path
        stroke-linecap="round"
        stroke-linejoin="round"
        stroke-width="2"
        d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
    </svg>
  </div>
  <div
    tabindex="0"
    class="$$card $$card-sm $$dropdown-content bg-base-100 rounded-box z-1 w-64 shadow-sm">
    <div tabindex="0" class="$$card-body">
      <h2 class="$$card-title">You needed more info?</h2>
      <p>Here is a description!</p>
    </div>
  </div>
</div>
A normal text and a helper dropdown
<div class="$$dropdown $$dropdown-end">
  <div tabindex="0" role="button" class="$$btn $$btn-circle $$btn-ghost $$btn-xs text-info">
    <svg
      tabindex="0"
      xmlns="http://www.w3.org/2000/svg"
      fill="none"
      viewBox="0 0 24 24"
      class="h-4 w-4 stroke-current">
      <path
        stroke-linecap="round"
        stroke-linejoin="round"
        stroke-width="2"
        d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
    </svg>
  </div>
  <div
    tabindex="0"
    class="$$card $$card-sm $$dropdown-content bg-base-100 rounded-box z-1 w-64 shadow-sm">
    <div tabindex="0" class="$$card-body">
      <h2 class="$$card-title">You needed more info?</h2>
      <p>Here is a description!</p>
    </div>
  </div>
</div>
daisyUI store

NEXUS
daisyUI 官方仪表盘模板

在 daisyUI 商店提供

更多详情