响应式滑动菜单

这段代码展示了如何使用HTML, CSS和JavaScript实现一个响应式的滑动菜单。菜单包括多个链接,每个链接都有一个图标,并且在不同屏幕尺寸下有不同的布局。当屏幕宽度小于520px时,菜单会隐藏,通过点击logo显示。在不同设备上,菜单项的排列和高度会根据屏幕大小自动调整,提供良好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title>响应式滑动菜单</title>
    <link rel="stylesheet" type="text/css" href="css/default.css" />
</head>
<style>
    .main {
        width: 1000px;
    }

    #ulList {
        display: flex;
        width: 100%;
    }

    ul li {
        transition: all .3s;
    }

    a::after {
        position: absolute;
        left: 0;
        top: 0;
        content: '';
        width: 100%;
        height: 100%;
        background-color: rgba(0, 0, 0, .2);
        z-index: 999;
    }

    li a {
        position: relative;
        display: flex !important;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
        transition: all .5s;
    }

    .icon {
        display: flex !important;
        justify-content: center;
        align-items: center;
        width: 50px;
        height: 50px;
        border-radius: 100%;
        background-color: rgb(209, 206, 206, .5);
        box-shadow: 0 0 30px 30px transparent;
        transition: all .5s ease-in-out;
    }

    li:hover .icon {
        box-shadow: 0 0 0px 0px rgba(238, 235, 235, 0.5);
    }

    li:hover a {
        justify-content: space-around;
    }

    #ulList li:hover a::after {
        z-index: -99;
    }

    @media screen and (min-width:520px) and (max-width:799px) {
        ul li {
            width: 50%;
            height: 100px;
        }

        li a {
            flex-direction: row;
            justify-content: space-between;
            padding: 0 50px;
        }

        #ulList li:hover {
            height: 150px;
        }

        #ulList {
            display: flex !important;
            flex-wrap: wrap;
            width: 700px;
        }

    }

    @media screen and (max-width:519px) {
        #ulList {
            flex-direction: column;
            width: 350px;
        }

        ul li {
            height: 50px;
        }

        li a {
            flex-direction: row;
            justify-content: space-between;
            padding: 0 50px;
        }

        .icon {
            width: 30px;
            height: 30px;
        }

        #imgLogo {
            display: block;
        }

        #ulList {
            display: none;
        }
    }

    @media screen and (min-width:800px) {
        ul li {
            display: flex;
            flex: 1;
            height: 200px;
        }

        #ulList li:hover {
            height: 250px;
        }

        #ulList {
            display: flex !important;
            width: 100%;
        }
    }
</style>

<body>
    <div class="container">
        <header>
            <h1>响应式滑动菜单</h1>
        </header>
        <div class="main clearfix">
            <nav id="menu" class="nav">
                <img id="imgLogo" src="images/1.png" alt="" />
                <ul id="ulList">
                    <li>
                        <a href="#">
                            <span class="icon">
                                <img src="images/2.png" alt="" />
                            </span>
                            <span>Home</span>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="icon">
                                <img src="images/3.png" alt="" />
                            </span>
                            <span>Services</span>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="icon">
                                <img src="images/4.png" alt="" />
                            </span>
                            <span>Portfolio</span>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="icon">
                                <img src="images/5.png" alt="" />
                            </span>
                            <span>Blog</span>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="icon">
                                <img src="images/6.png" alt="" />
                            </span>
                            <span>The team</span>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="icon">
                                <img src="images/7.png" alt="" />
                            </span>
                            <span>Contact</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
    <script src="js/jquery-1.12.4.js"></script>
    <script>
        $('#imgLogo').click(function () {
            $('#ulList').toggle();
        })
    </script>
</body>

</html>

default.css


*, *:after, *:before {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}

body, html {
    font-size: 15px;
    padding: 0;
    margin: 0;
}
.clearfix:after {
    content: " ";
    display: block;
    clear: both;
}
body {
    font-family: 'Lato', Calibri, Arial, sans-serif;
    color: #89867e;
    background: #f9f9f9;
}

a {
    color: #333;
    text-decoration: none;
}

a:hover {
    color: #fff;
}
.main, .container > header {
    width: 100%;
    margin: 0 auto;
    padding: 30px;
}
.main {
    max-width: 1024px;
    min-height: 600px;
}
.container > header {
    text-align: center;
    font-size: 16px;
    background: rgba(0, 0, 0, 0.01);
}
.container > header h1 {
    font-size: 30px;
    line-height: 1.3;
    margin: 0;
    font-weight: normal;
}

.nav ul {
    max-width: 1240px;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: 24px;
    font-weight: 300;
}
.nav li span {
    display: block;
}
.nav a {
    display: block;
    color: rgba(249, 249, 249, .9);
    text-decoration: none;
}
#imgLogo{
    display: none;
}
a, button {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
.nav li:nth-child(6n+1) {
    background: rgb(208, 101, 3);
}

.nav li:nth-child(6n+2) {
    background: rgb(233, 147, 26);
}

.nav li:nth-child(6n+3) {
    background: rgb(22, 145, 190);
}

.nav li:nth-child(6n+4) {
    background: rgb(22, 107, 162);
}
.nav li:nth-child(6n+5) {
    background: rgb(27, 54, 71);
}
.nav li:nth-child(6n+6) {
    background: rgb(21, 40, 54);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值