MDUI文档
复制 llms.txt 链接复制 llms-full.txt 链接以 Markdown 格式查看此页与 ChatGPT 讨论此页内容与 ChatGPT 讨论项目完整文档
预设颜色
自选颜色
从壁纸提取颜色
请选择一张壁纸
开发指南
AI 辅助开发
样式
与框架集成
组件
Avatar 头像Badge 徽标BottomAppBar 底部应用栏Button 按钮ButtonIcon 图标按钮Card 卡片Checkbox 复选框Chip 标签CircularProgress 圆形进度条Collapse 折叠面板Dialog 对话框Divider 分隔线Dropdown 下拉组件Fab 浮动操作按钮Icon 图标Layout 布局LinearProgress 线性进度条List 列表Menu 菜单NavigationBar 底部导航栏NavigationDrawer 抽屉导航栏NavigationRail 侧边导航栏Radio 单选框RangeSlider 范围滑块Select 选择框SegmentedButton 分段按钮Slider 滑块Snackbar 消息条Switch 开关切换Tabs 选项卡TextField 文本框Tooltip 工具提示TopAppBar 顶部应用栏
函数
独立包

NavigationDrawer抽屉导航栏

The navigation drawer provides side navigation between pages on a website.

Typically, the <mdui-list> component is used within the navigation drawer to add navigation items.

Usage

Import the component:

import 'mdui/components/navigation-drawer.js';

Import the TypeScript type:

import type { NavigationDrawer } from 'mdui/components/navigation-drawer.js';

Example:

Close Navigation Drawer Open Navigation Drawer
<mdui-navigation-drawer close-on-overlay-click class="example-drawer">
  <mdui-button>Close Navigation Drawer</mdui-button>
</mdui-navigation-drawer>

<mdui-button>Open Navigation Drawer</mdui-button>

<script>
  const navigationDrawer = document.querySelector(".example-drawer");
  const openButton = navigationDrawer.nextElementSibling;
  const closeButton = navigationDrawer.querySelector("mdui-button");

  openButton.addEventListener("click", () => navigationDrawer.open = true);
  closeButton.addEventListener("click", () => navigationDrawer.open = false);
</script>

Notes:

By default, this component uses position: fixed.

When modal is false and the breakpoint is at least --mdui-breakpoint-md, it automatically adds padding-left or padding-right to the body to prevent content from being obscured.

However, it uses a position: absolute style in the following cases:

  1. When the contained property is true.
  2. When the component is used inside the <mdui-layout></mdui-layout> component. In this case, padding-left or padding-right is not added.

Examples

In Container

By default, the navigation drawer appears on the left or right side of the viewport. To place it inside a container, add the contained attribute. This makes the navigation drawer position itself relative to the parent element (you need to add position: relative; overflow: hidden; styles to the parent element).

Modal

The modal attribute displays a modal overlay when the navigation drawer is open. Note that if the window or parent element width is less than --mdui-breakpoint-md, this attribute is ignored and the modal overlay is always displayed.

The close-on-esc attribute lets the navigation drawer close when the ESC key is pressed.

The close-on-overlay-click attribute lets the navigation drawer close when the modal overlay is clicked.

Right Placement

Use the placement attribute to place the navigation drawer on the right side.

API

属性

HTML 属性JavaScript 属性Reflect类型默认值
openopenbooleanfalse

Opens the navigation drawer.

modalmodalbooleanfalse

Displays an overlay when open.

On narrow devices (screen width < --mdui-breakpoint-md), the overlay is always displayed.

close-on-esccloseOnEscbooleanfalse

Closes the drawer when the ESC key is pressed and an overlay is present.

close-on-overlay-clickcloseOnOverlayClickbooleanfalse

Closes the drawer when the overlay is clicked.

placementplacement'left' | 'right''left'

Sets the drawer's position. Possible values:

  • left: Displays on the left side.
  • right: Displays on the right side.
containedcontainedbooleanfalse

By default, the navigation drawer is positioned relative to the body element. If set, it is positioned relative to its parent element.

Note: You must manually set position: relative; overflow: hidden; on the parent element when this attribute is set.

orderordernumber-

Specifies the layout order within the <mdui-layout> component. Items are sorted in ascending order. The default value is 0.

事件

名称
open

Emitted when the navigation drawer starts to open. Can be prevented with event.preventDefault().

opened

Emitted after the navigation drawer has opened and the animations are completed.

close

Emitted when the navigation drawer starts to close. Can be prevented with event.preventDefault().

closed

Emitted after the navigation drawer has closed and the animations are completed.

overlay-click

Emitted when the overlay is clicked.

Slots

名称
默认

Contents of the navigation drawer.

CSS Parts

名称
overlay

Overlay element.

panel

Container for the navigation drawer.

CSS 自定义属性

名称
--shape-corner

The corner radius of the component. You can use a specific pixel value, but it is recommended to reference design tokens.

--z-index

The CSS z-index value of the component.

本页目录