首页 > 解决方案 > 导航栏下的新内容在关闭后最终在导航栏内

问题描述

标签: htmlcss

解决方案


我已经删除了浮动元素logonav-wrapper以避免与另一个 div 重叠。我使用 flex-box 来对齐 logo 和菜单。

并调整了响应式媒体查询。我希望这个解决方案对您有所帮助。

* {
    box-sizing: border-box;
}

html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100vh;
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100vh;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    font-weight: 400;
    background: url(https://via.placeholder.com/1920x600);
    background-position: center;
    background-size: cover;
}

.container {
    width: 100%;
    padding: 0 20px;
}

nav {
    display: flex;
}

.logo img {
    max-width: 100%;
}

.logo {
    padding: 10px 0;
    /* float: left;
      margin-left: 16px;
      margin-top: 8px; */
}

.logo a {
    color: rgb(255, 255, 255);
    text-transform: uppercase;
    font-weight: 700;
    font-size: 18px;
    letter-spacing: 0px;
    text-decoration: none;
}

.nav-wrapper {
    margin-left: auto;
}

nav ul li {
    display: inline-block;
}

nav ul li:not(:first-child) {
    margin-left: 48px;
}

nav ul li:last-child {
    margin-right: 24px;
}

nav ul li a {
    display: inline-block;
    outline: none;
    color: rgb(255, 255, 255);
    text-transform: uppercase;
    text-decoration: none;
    font-size: 14px;
    letter-spacing: 1.2px;
    font-weight: 600;
}

@media screen and (max-width: 864px) {
    .nav-wrapper {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: -1;
        background: rgba(71, 8, 71, 0.507);
        opacity: 0;
        transition: all 0.2s ease;
    }
    .nav-wrapper ul {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 100%;
        margin-top: 30px;
    }
    .nav-wrapper ul li {
        display: block;
        float: none;
        width: 100%;
        text-align: right;
        margin-bottom: 10px;
    }
    .nav-wrapper ul li:nth-child(1) a {
        transition-delay: 0.2s;
    }
    .nav-wrapper ul li:nth-child(2) a {
        transition-delay: 0.3s;
    }
    .nav-wrapper ul li:nth-child(3) a {
        transition-delay: 0.4s;
    }
    .nav-wrapper ul li:nth-child(4) a {
        transition-delay: 0.5s;
    }
    .nav-wrapper ul li:not(:first-child) {
        margin-left: 0;
    }
    .nav-wrapper ul li a {
        padding: 10px 24px;
        opacity: 0;
        color: rgb(255, 255, 255);
        font-size: 14px;
        font-weight: 600;
        letter-spacing: 1.2px;
        transform: translateX(-20px);
        transition: all 0.2s ease;
    }
    .nav-btn {
        position: fixed;
        right: 15px;
        top: 25px;
        display: block;
        width: 30px;
        height: 40px;
        cursor: pointer;
        z-index: 9999;
        border-radius: 50%;
    }
    .nav-btn i {
        position: fixed;
        display: block;
        width: 20px;
        height: 2px;
        background: rgb(255, 255, 255);
        border-radius: 2px;
        right: 15px;
    }
    .nav-btn i:nth-child(2) {
        margin-top: 6px;
        opacity: 1;
    }
    .nav-btn i:nth-child(3) {
        margin-top: 12px;
    }
}

#nav:checked + .nav-btn {
    transform: rotate(45deg);
}

#nav:checked + .nav-btn i {
    background: rgb(255, 255, 255);
    transition: transform 0.2s ease;
}

#nav:checked + .nav-btn i:nth-child(1) {
    transform: translateY(6px) rotate(180deg);
}

#nav:checked + .nav-btn i:nth-child(2) {
    opacity: 0;
}

#nav:checked + .nav-btn i:nth-child(3) {
    transform: translateY(-6px) rotate(90deg);
}

#nav:checked ~ .nav-wrapper {
    z-index: 9990;
    opacity: 1;
}

#nav:checked ~ .nav-wrapper ul li a {
    opacity: 1;
    transform: translateX(0);
}

.hidden {
    display: none;
}
<div class="container">
    <nav>
        <input type="checkbox" id="nav" class="hidden">
        <label for="nav" class="nav-btn">
            <i></i>
            <i></i>
            <i></i>
        </label>
        <div class="logo">
            <a href="#"><img src="https://placeholder.com/wp-content/uploads/2018/10/placeholder.com-logo1.png" width="140"></a>
        </div>
        <div class="nav-wrapper">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Events</a></li>
                <li><a href="#">Artists</a></li>
                <li><a href="#">Email</a></li>
            </ul>
        </div>
    </nav>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
</div>


推荐阅读