响应式全屏导航

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "PingFang SC", "Microsoft Yahei", sans-serif;
        }
        
        header {
            width: 100vw;
            height: 100vh;
            background-image: url("https://ts4.cn.mm.bing.net/th?id=OIP-C.zErZ2EYur9dN3D13JPC23gHaE8&w=306&h=204&c=8&rs=1&qlt=90&o=6&dpr=1.3&pid=3.1&rm=2");
            background-size: cover;
        }
        
        nav {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 5vw;
            background-color: rgba(65, 81, 101, 0.9);
            height: 80px;
        }
        
        .logo {
            font-size: 24px;
            font-weight: 600;
            color: #dadbdd;
            flex: 1;
        }
        
        .nav-menu {
            flex: 1;
            display: flex;
            justify-content: space-between;
            max-width: 550px;
        }
        
        .nav-menu li {
            list-style: none;
            color: #dadbdd;
            font-weight: bold;
        }
        
        .burger div {
            width: 25px;
            height: 3px;
            background-color: #dadbdd;
            margin: 4px;
        }
        
        .burger {
            display: none;
        }
        /* 屏幕适配 */
        
        @media screen and (max-width:768px) {
            .nav-menu {
                position: absolute;
                top: 80px;
                right: 0;
                bottom: 0;
                width: 50vw;
                height: calc(100vh - 80px);
                background-color: rgba(65, 81, 101, 0.9);
                flex-direction: column;
                align-items: center;
                justify-content: flex-start;
                transform: translateX(100%);
                transition: 0.4s ease-in-out;
            }
            .nav-menu.open {
                transform: translateX(0);
                transition: 0.4s ease-in-out;
            }
            .nav-menu li {
                margin: 3vh;
                transform: translateX(20px);
                opacity: 0;
            }
            .burger {
                display: block;
            }
            .burger.active div {
                transition: 0.3s ease-in-out 0.3s;
            }
            .burger.active .top-line {
                transform: rotate(45deg) translate(4px, 6px);
            }
            .burger.active .bottom-line {
                transform: rotate(-45deg) translate(4px, -6px);
            }
            .burger.active .middle-line {
                opacity: 0;
                transform: translateX(10px);
                transition: 0.3s ease-in-out;
            }
            @keyframes slideIn {
                from {
                    transform: translateX(20px);
                    opacity: 0;
                }
                to {
                    transform: translateX(0);
                    opacity: 1;
                }
            }
        }
    </style>
</head>

<body>
    <nav>
        <div class="logo">大前端时代</div>
        <ul class="nav-menu">
            <li>首页</li>
            <li>关于我</li>
            <li>我的作品</li>
            <li>我的简历</li>
            <li>我的博客</li>
        </ul>
        <div class="burger">
            <div class="top-line"></div>
            <div class="middle-line"></div>
            <div class="bottom-line"></div>
        </div>
    </nav>
    <header> </header>
    <script>
        const burger = document.querySelector(".burger");
        const navMenu = document.querySelector(".nav-menu");

        const navMenuItems = document.querySelectorAll(".nav-menu li")
        burger.addEventListener("click", () => {
            burger.classList.toggle("active");
            navMenu.classList.toggle("open");

            navMenuItems.forEach((item, index) => {
                if (item.style.animation) {
                    item.style.animation = "";
                } else {
                    item.style.animation = `0.3s ease-in slideIn  forwards  ${index*0.1+0.3}s`;
                }
            })

        })
    </script>
</body>

</html>

效果图:


响应式全屏导航_第1张图片
响应式全屏导航_第2张图片

你可能感兴趣的:(css,前端)