CSS二级导航菜单的实现

用CSS实现二级导航菜单

  • 效果如下
    CSS二级导航菜单的实现_第1张图片

  • 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="">一级菜单1a>
                li>
                <li>
                    <a href="">一级菜单2a>
                    <ol>
                        <li><a href="">二级菜单1a>li>
                        <li><a href="">二级菜单2a>li>
                        <li><a href="">二级菜单3a>li>
                        <li><a href="">二级菜单4a>li>
                        <li><a href="">二级菜单5a>li>
                        <li><a href="">二级菜单6a>li>
                    ol>
                li>
                <li>
                    <a href="">一级菜单3a>
                    <ol>
                        <li><a href="">二级菜单1a>li>
                        <li><a href="">二级菜单2a>li>
                        <li><a href="">二级菜单3a>li>
                        <li><a href="">二级菜单4a>li>
                        <li><a href="">二级菜单5a>li>
                        <li><a href="">二级菜单6a>li>
                    ol>
                li>
                <li>
                    <a href="">一级菜单4a>
                    <ol>
                        <li><a href="">二级菜单1a>li>
                        <li><a href="">二级菜单2a>li>
                        <li><a href="">二级菜单3a>li>
                        <li><a href="">二级菜单4a>li>
                        <li><a href="">二级菜单5a>li>
                        <li><a href="">二级菜单6a>li>
                    ol>
                li>
                <li>
                    <a href="">一级菜单5a>
                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;
}

你可能感兴趣的:(笔记,html,css)