CSS二级导航菜单的实现

用CSS实现二级导航菜单

  • 效果如下
    在这里插入图片描述

  • HTML代码

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSS二级导航菜单</title>
    <link rel="stylesheet" href="../css/nav.css">
</head>

<body>
    <div class="navbar">
        <div class="nav">
            <ul>
                <li>
                    <a href="">一级菜单1</a>
                </li>
                <li>
                    <a href="">一级菜单2</a>
                    <ol>
                        <li><a href="">二级菜单1</a></li>
                        <li><a href="">二级菜单2</a></li>
                        <li><a href="">二级菜单3</a></li>
                        <li><a href="">二级菜单4</a></li>
                        <li><a href="">二级菜单5</a></li>
                        <li><a href="">二级菜单6</a></li>
                    </ol>
                </li>
                <li>
                    <a href="">一级菜单3</a>
                    <ol>
                        <li><a href="">二级菜单1</a></li>
                        <li><a href="">二级菜单2</a></li>
                        <li><a href="">二级菜单3</a></li>
                        <li><a href="">二级菜单4</a></li>
                        <li><a href="">二级菜单5</a></li>
                        <li><a href="">二级菜单6</a></li>
                    </ol>
                </li>
                <li>
                    <a href="">一级菜单4</a>
                    <ol>
                        <li><a href="">二级菜单1</a></li>
                        <li><a href="">二级菜单2</a></li>
                        <li><a href="">二级菜单3</a></li>
                        <li><a href="">二级菜单4</a></li>
                        <li><a href="">二级菜单5</a></li>
                        <li><a href="">二级菜单6</a></li>
                    </ol>
                </li>
                <li>
                    <a href="">一级菜单5</a>
                </li>
                <!-- 滑动线条,这里需要设置滑动条的三角形 -->
                <li class="underline"></li>
            </ul>
        </div>
    </div>
</body>

</html>
  • CSS代码
* {
    margin: 0;
    padding: 0;
}

body {
    width: 100vm;
    background: #f2f2f2;
}

li {
    list-style: none;
}

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

.navbar {
    width: 100%;
    height: 70px;
    background: #fff;
    box-shadow: 0 0 5px rgba(0, 0, 0, .3);
}

.navbar .nav {
    width: 1200px;
    height: 100%;
    margin: 0 auto;
}

.navbar .nav ul {
    position: relative;
    display: flex;
    justify-content: space-around;
    width: 100%;
    height: 100%;
}

.navbar .nav ul>li {
    width: 100%;
    height: 100%;
}

.navbar .nav ul>li>a {
    display: block;
    width: 100%;
    height: 100%;
    line-height: 70px;
    text-align: center;
}

.navbar .nav ul>li>ol {
    width: 100%;
    background: #fff;
    box-shadow: 1px 1px 3px rgba(0, 0, 0, .2);
    transform: scaleY(0);
    transition: all 0.6s;
    transform-origin: 50% 0;
}

.navbar .nav ul>li>ol>li {
    height: 70px;
    border-bottom: 1px solid rgb(245, 245, 245);
}

.navbar .nav ul>li>ol>li>a {
    display: block;
    width: 100%;
    height: 100%;
    line-height: 70px;
    text-align: center;
}

.navbar .nav ul>li>ol>li:hover {
    background: rgba(0, 0, 0, 0.03);
}

.navbar .nav ul li:hover ol {
    transform: scaleY(1);
}

/* 设置线条 */
.navbar .nav ul .underline {
    position: absolute;
    left: 0px;
    bottom: 0;
    width: 240px;
    height: 6px;
    background: red;
    border-top-left-radius: 6px;
    border-top-right-radius: 6px;
    transition: all 0.5s;
    pointer-events: none;
}

/* 边框三角的实现方法一 */
.navbar .nav ul .underline::before {
    content: "";
    position: absolute;
    width: 18px;
    height: 10px;
    top: -10px;
    left: calc(50% - 9px);
    background: inherit;
    clip-path: polygon(0 100%, 50% 0, 100% 100%);
}

/* 边框三角的实现方法二*/
/* .navbar .nav ul .underline::before{
    content: "";
    position: absolute;
    display: block;
    top: -20px;
    left: calc(50% - 9px);
    border: 10px solid transparent;
    border-bottom-color: red;
} */
.navbar .nav ul>li:nth-child(2):hover~.underline {
    left: 240px;
}

.navbar .nav ul>li:nth-child(3):hover~.underline {
    left: 480px;
}

.navbar .nav ul>li:nth-child(4):hover~.underline {
    left: 720px;
}

.navbar .nav ul>li:nth-child(5):hover~.underline {
    left: 960px;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值