示例工程 33-dialog-popover
33-dialog-popover/demo-02-popover/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>popover 演示</title>
<style>
body { margin: 0; padding: 32px; background: #f8fafc; color: #0f172a; font-family: system-ui, sans-serif; line-height: 1.7; }
.box { max-width: 720px; margin-bottom: 16px; padding: 18px; border: 1px solid #e2e8f0; border-radius: 12px; background: #fff; }
h1 { margin: 0 0 16px; font-size: 28px; }
h2 { margin: 0 0 8px; font-size: 17px; }
p { margin: 0 0 10px; color: #334155; }
button { padding: 7px 14px; border: 1px solid #2563eb; border-radius: 6px; background: #2563eb; color: #fff; font: inherit; }
button.secondary { background: #fff; color: #334155; border-color: #cbd5e1; }
[popover] { width: 260px; padding: 12px; border: 1px solid #cbd5e1; border-radius: 8px; background: #fff; }
[popover] ul { margin: 0; padding-left: 18px; }
code { padding: 1px 5px; border-radius: 4px; background: #f1f5f9; font-family: ui-monospace, monospace; }
</style>
</head>
<body>
<h1>popover</h1>
<div class="box">
<h2>auto</h2>
<button type="button" popovertarget="auto-menu">打开菜单</button>
<div id="auto-menu" popover="auto">
<ul>
<li><a href="#top">项目一</a></li>
<li><a href="#top">项目二</a></li>
</ul>
</div>
<p>点击外部或 Esc 自动关闭。</p>
</div>
<div class="box">
<h2>manual</h2>
<button type="button" popovertarget="manual-panel">打开面板</button>
<div id="manual-panel" popover="manual">
<p>手动关闭,不会因点击外部而消失。</p>
<button type="button" popovertarget="manual-panel" popovertargetaction="hide" class="secondary">关闭</button>
</div>
<p>适合需要用户确认的浮层,但确认操作仍建议用 dialog。</p>
</div>
</body>
</html>