浏览器原生Popover API弹出层用法示例代码

代码语言:html

所属分类:弹出层

代码描述:浏览器原生Popover API弹出层用法示例代码

代码标签: 浏览器 原生 Popover API 弹出层 用法 示例 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>原生 Popover API 完整示例</title>
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
        }
        body {
            padding: 40px;
            line-height: 1.6;
            color: #1f2937;
            background-color: #f9fafb;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            gap: 40px;
            display: flex;
            flex-direction: column;
        }
        .section {
            background: white;
            padding: 24px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
        }
        h2 {
            margin-bottom: 20px;
            color: #111827;
            font-size: 20px;
            font-weight: 600;
            border-bottom: 1px solid #e5e7eb;
            padding-bottom: 12px;
        }
        .demo-group {
            display: flex;
            flex-wrap: wrap;
            gap: 16px;
            margin-bottom: 24px;
        }
        /* 通用按钮样式 */
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.15s;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }
        .btn-primary {
            background: #3b82f6;
            color: white;
        }
        .btn-primary:hover {
            background: #2563eb;
        }
        .btn-danger {
            background: #dc2626;
            color: white;
        }
        .btn-danger:hover {
            background: #b91c1c;
        }
        .btn-secondary {
            background: #f3f4f6;
            color: #374151;
        }
        .btn-secondary:hover {
            background: #e5e7eb;
        }
        .icon-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #f3f4f6;
            border: none;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #1f2937;
            transition: background 0.15s;
        }
        .icon-btn:hover {
            background: #e5e7eb;
        }

        /* 1. 基础示例样式 */
        .basic-popover {
            padding: 20px;
            border-radius: 8px;
            background: white;
            border: 1px solid #e5e7eb;
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
            min-width: 280px;
        }

        /* 2. 用户头像下拉菜单样式 */
        .main-nav {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
        }
        .nav-trigger {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px 12px;
            background: transparent;
            border: 1px solid #e5e7eb;
            border-radius: 6px;
            cursor: pointer;
            transition: background 0.15s;
        }
        .nav-trigger:hover {
            background: #f9fafb;
        }
        .avatar {
            width: 32px;
            height: 32px;
            border-radius: 50%;
            object-fit: cover;
        }
        .chevron {
            transition: transform 0.2s;
        }
        .nav-trigger:has(+ [popover]:popover-open) .chevron {
            transform: rotate(180deg);
        }
        .dropdown-menu {
            margin: 0;
            padding: 0;
            border: 1px solid #e5e7eb;
            border-radius: 8px;
            background: white;
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
            min-width: 200px;
        }
        .menu-item {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 16px;
            color: #1f2937;
            text-decoration: none;
            transition: background 0.15s;
        }
        .menu-item:hover {
            background: #f3f4f6;
        }
        .menu-item:first-child {
            border-radius: 8px 8px 0 0;
        }
        .menu-item:last-child {
            border-radius: 0 0 8px 8px;
        }
        .menu-item--danger {
            color: #dc2626;
        }
        .menu-divider {
            margin: 4px 0;
            border: none;
            border-top: 1px solid #e5e7eb;
        }

        /* 3. 确认弹窗样式 */
        .modal {
            position: fixed;
            top: 50%;
            left: 50%;
            translate: -50% -50%;
            width: 90%;
            max-width: 450px;
            margin: 0;
            padding: 0;
            border: 1px solid #e5e7eb;
            border-radius: 12px;
            background: white;
            box-shadow: 0 20px 50px rgba(0,0,0,0.15);
            opacity: 0;
            transform: scale(0.95);
            transition: opacity 0.2s, transform 0.2s,
                        overlay 0.2s allow-discrete,
                        display 0.2s allow-discrete;
        }
        .modal:popover-open {
            opacity: 1;
            transform: scale(1);
        }
        @starting-style {
            .modal:popover-open {
                opacity: 0;
                transform: scale(0.95);
            }
        }
        .modal::backdrop {
            background: rgba(0,0,0,0.5);
            backdrop-filter: blur(4px);
            opacit.........完整代码请登录后点击上方下载按钮下载查看

网友评论0