CSS动画导航菜单

纯CSS实现动画导航菜单

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS动画导航菜单</title>
    <style>
        /* 基础样式重置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }
        
        body {
            background-color: #f5f5f5;
            padding: 20px;
        }
        
        /* 导航菜单容器 */
        .nav-container {
            max-width: 1200px;
            margin: 0 auto;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        
        /* 导航菜单样式 */
        .navbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
            background: linear-gradient(135deg, #6e8efb, #a777e3);
        }
        
        .logo {
            color: white;
            font-size: 24px;
            font-weight: bold;
            text-decoration: none;
            padding: 15px 0;
        }
        
        .nav-links {
            display: flex;
            list-style: none;
        }
        
        .nav-links li {
            position: relative;
        }
        
        .nav-links a {
            color: white;
            text-decoration: none;
            padding: 20px 15px;
            display: block;
            transition: all 0.3s ease;
            position: relative;
        }
        
        /* 悬停动画效果 */
        .nav-links a::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 50%;
            width: 0;
            height: 3px;
            background: white;
            transition: all 0.3s ease;
            transform: translateX(-50%);
        }
        
        .nav-links a:hover::after {
            width: 80%;
        }
        
        /* 下拉菜单 */
        .dropdown {
            position: relative;
        }
        
        .dropdown-content {
            position: absolute;
            top: 100%;
            left: 0;
            background: white;
            width: 200px;
            border-radius: 0 0 8px 8px;
            box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
            opacity: 0;
            visibility: hidden;
            transform: translateY(10px);
            transition: all 0.3s ease;
            z-index: 1;
        }
        
        .dropdown:hover .dropdown-content {
            opacity: 1;
            visibility: visible;
            transform: translateY(0);
        }
        
        .dropdown-content a {
            color: #333;
            padding: 12px 15px;
            border-bottom: 1px solid #eee;
        }
        
        .dropdown-content a:hover {
            background: #f5f5f5;
            color: #6e8efb;
        }
        
        .dropdown-content a::after {
            display: none;
        }
        
        /* 响应式设计 - 移动端菜单 */
        .menu-toggle {
            display: none;
            cursor: pointer;
            padding: 15px;
        }
        
        .menu-toggle span {
            display: block;
            width: 25px;
            height: 3px;
            background: white;
            margin: 5px 0;
            transition: all 0.3s ease;
        }
        
        /* 移动端样式 */
        @media (max-width: 768px) {
            .navbar {
                flex-direction: column;
                align-items: flex-start;
                padding: 0;
            }
            
            .logo {
                padding: 15px 20px;
            }
            
            .menu-toggle {
                display: block;
                position: absolute;
                top: 15px;
                right: 20px;
            }
            
            .nav-links {
                flex-direction: column;
                width: 100%;
                max-height: 0;
                overflow: hidden;
                transition: max-height 0.3s ease;
            }
            
            .nav-links.active {
                max-height: 500px;
            }
            
            .nav-links li {
                width: 100%;
            }
            
            .nav-links a {
                padding: 15px 20px;
                border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            }
            
            .dropdown-content {
                position: static;
                width: 100%;
                display: none;
                box-shadow: none;
                border-radius: 0;
            }
            
            .dropdown:hover .dropdown-content {
                display: block;
                opacity: 1;
                visibility: visible;
                transform: none;
            }
            
            /* 菜单图标动画 */
            .menu-toggle.active span:nth-child(1) {
                transform: translateY(8px) rotate(45deg);
            }
            
            .menu-toggle.active span:nth-child(2) {
                opacity: 0;
            }
            
            .menu-toggle.active span:nth-child(3) {
                transform: translateY(-8px) rotate(-45deg);
            }
        }
        
        /* 内容区域样式 */
        .content {
            padding: 40px 20px;
            text-align: center;
        }
        
        .content h1 {
            margin-bottom: 20px;
            color: #333;
        }
        
        .content p {
            color: #666;
            line-height: 1.6;
            margin-bottom: 30px;
        }
        
        .download-btn {
            display: inline-block;
            padding: 12px 30px;
            background: linear-gradient(135deg, #6e8efb, #a777e3);
            color: white;
            text-decoration: none;
            border-radius: 50px;
            font-weight: bold;
            box-shadow: 0 4px 15px rgba(106, 142, 251, 0.4);
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        
        .download-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(106, 142, 251, 0.6);
        }
        
        .download-btn::after {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: all 0.5s ease;
        }
        
        .download-btn:hover::after {
            left: 100%;
        }
        
        /* 页脚样式 */
        footer {
            text-align: center;
            padding: 20px;
            color: #666;
            font-size: 14px;
        }
        
        footer a {
            color: #6e8efb;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="nav-container">
        <nav class="navbar">
            <a href="#" class="logo">CSS动画导航</a>
            <div class="menu-toggle">
                <span></span>
                <span></span>
                <span></span>
            </div>
            <ul class="nav-links">
                <li><a href="#">首页</a></li>
                <li><a href="#">关于我们</a></li>
                <li class="dropdown">
                    <a href="#">服务</a>
                    <div class="dropdown-content">
                        <a href="#">网页设计</a>
                        <a href="#">UI/UX设计</a>
                        <a href="#">前端开发</a>
                        <a href="#">SEO优化</a>
                    </div>
                </li>
                <li><a href="#">案例展示</a></li>
                <li><a href="#">联系我们</a></li>
            </ul>
        </nav>
    </div>
    
    <div class="content">
        <h1>纯CSS动画导航菜单</h1>
        <p>这是一个完全使用CSS实现的动画导航菜单,包含悬停效果、下拉菜单和响应式设计。</p>
        <a href="#" class="download-btn">下载完整代码</a>
    </div>
    
    <footer>
        <p>© 2023 CSS动画导航菜单</p>
    </footer>
    
    <script>
        // 移动端菜单切换
        document.querySelector('.menu-toggle').addEventListener('click', function() {
            this.classList.toggle('active');
            document.querySelector('.nav-links').classList.toggle('active');
        });
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值