<button popovertarget="my-popover" class="trigger-btn">打开弹窗</button>
<div id="my-popover" popover=manual>
<p>我是一个包含一些信息的弹窗。按下❌按钮即可将我关闭<p>
<button class="close-btn" popovertarget="my-popover"
popovertargetaction="hide">
<span aria-hidden="true">❌</span>
</button>
</div>
HTML
格式化
支持Emmet,输入 p 后按 Tab键试试吧!
<head> ... </head>
<body>
</body>
button {
font-size: 100%;
padding: 0.75rem 1.5rem;
background: white;
transition-duration: 0.5s;
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;
border-radius: 1rem;
max-width: 22ch;
line-height: 1.4;
top: 2rem;
margin: 0 auto;
gap: 1rem;
}
[popover]:popover-open {
display: flex;
}
body {
background: #fcf9fb;
display: grid;
font-size: 1.5rem;
font-family: system-ui, sans-serif;
place-items: center;
height: 100dvh;
}
/* 关闭按钮 */
.close-btn {
border: none;
background: none;
justify-self: end;
font-size: 1.5rem;
border-radius: 0.8rem;
[aria-hidden] {
filter: grayscale() brightness(10);
}
&:hover,
&:focus {
background: plum;
filter: none;
}
}
/* 你还可以给带有 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;
}
}
预览
控制台