<button popovertarget="my-popover">打开弹窗</button>
<div id="my-popover" popover>
<p>我是一个包含一些信息的弹窗。 按下 <kbd>Esc</kbd> 键或点击弹窗外部将我关闭<p>
</div>
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ... </head>
<body>
</body>
button {
font-size: 100%;
padding: 0.75rem 1.5rem;
transition-duration: 0.2s;
border: 4px solid plum;
background: lavenderblush;
border-radius: 1rem;
&:hover,
&:focus {
background: plum;
color: white;
}
}
[popover] {
background: black;
color: white;
font-weight: 400;
padding: 1rem 1.5rem;
border-radius: 1rem;
max-width: 20ch;
line-height: 1.4;
top: 2rem;
margin: 0 auto;
}
body {
background: #fcf9fb;
display: grid;
font-size: 1.5rem;
font-family: system-ui, sans-serif;
place-items: center;
height: 100dvh;
}
/* 你还可以给带有 popover 属性的元素设置过度动画 */
/* 打开时 */
[popover]:popover-open {
translate: 0 0;
}
/* 关闭时 */
[popover] {
transition: translate 0.3s ease-out, display 0.3s ease-out allow-discrete;
translate: 0 -22rem;
}
/* 打开之前 */
@starting-style {
[popover]:popover-open {
translate: 0 -22rem;
}
}
预览
控制台