示例工程 31-modals-dropdowns
31-modals-dropdowns/app/index.html
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>弹层与下拉</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="app">
<header class="site-header">
<span class="brand">OVERLAY LAB</span>
<nav aria-label="页面导航">
<a href="#dropdown">下拉</a>
<a href="#dialog">对话框</a>
<a href="#notes">要点</a>
</nav>
</header>
<main class="page">
<p class="eyebrow">LESSON / 31</p>
<h1>弹层要有边界,也要能退出</h1>
<p class="lead">
下拉用 details / popover,模态用 dialog;Esc、焦点与滚动锁定都要能工作。
</p>
<section class="panel" id="dropdown">
<h2>1. 下拉菜单</h2>
<p>左边用 details,右边用 popover;都能用键盘打开。</p>
<div class="dropdown-grid">
<article class="demo">
<h3>details + summary</h3>
<details class="menu">
<summary>排序方式</summary>
<ul class="menu-panel">
<li><a href="#dropdown">最新优先</a></li>
<li><a href="#dropdown">价格从低到高</a></li>
<li><a href="#dropdown">销量优先</a></li>
</ul>
</details>
<p class="hint">无需 JS;点击外部不会自动关闭。</p>
</article>
<article class="demo">
<h3>popover</h3>
<button class="btn" type="button" popovertarget="filter-panel">筛选</button>
<div id="filter-panel" class="filter-panel" popover>
<p class="filter-title">筛选条件</p>
<label class="check"><input type="checkbox" checked /> 只看免费</label>
<label class="check"><input type="checkbox" /> 只看进阶</label>
<button class="btn primary" type="button" popovertarget="filter-panel">应用</button>
</div>
<p class="hint">Esc 关闭、点外部关闭;非模态。</p>
</article>
</div>
</section>
<section class="panel" id="dialog">
<h2>2. 模态对话框</h2>
<p>dialog 进入 top layer,背景不可交互;Esc 关闭并把焦点还给按钮。</p>
<button class="btn primary" id="open-dialog" type="button">打开对话框</button>
<dialog id="demo-dialog" class="dialog" aria-labelledby="dialog-title">
<form method="dialog" class="dialog-head">
<h2 id="dialog-title">确认发布</h2>
<button class="icon-btn" type="submit" aria-label="关闭">×</button>
</form>
<div class="dialog-body">
<p>发布后所有订阅者都会收到通知,且无法撤回。</p>
<label class="field">
<span class="label">发布说明(可选)</span>
<input class="input" type="text" placeholder="一句话说明" />
</label>
</div>
<div class="dialog-foot">
<form method="dialog">
<button class="btn" type="submit">取消</button>
</form>
<form method="dialog">
<button class="btn primary" type="submit">确认发布</button>
</form>
</div>
</dialog>
</section>
<section class="panel" id="notes">
<h2>3. 要点</h2>
<ul class="notes">
<li>模态用 <code>dialog.showModal()</code>,不要用 z-index 模拟。</li>
<li>下拉优先 <code>popover</code> 或 <code>details</code>,不要 hover 菜单。</li>
<li>关闭后焦点回到触发按钮。</li>
<li>背景滚动锁定,关闭时恢复。</li>
</ul>
</section>
</main>
<footer class="site-footer">
<p>示例数据,仅用于教程演示。</p>
<p>MODALS & DROPDOWNS · 2026</p>
</footer>
</div>
<script>
// 可跳过:仅用于打开 dialog;关闭/Esc 由原生 form method="dialog" 与浏览器处理
const dialog = document.querySelector("#demo-dialog");
document.querySelector("#open-dialog").addEventListener("click", () => dialog.showModal());
</script>
</body>
</html>