导航栏下拉功能

时间:2020-06-30 21:50:45

标签: html css navbar responsive

我有一个导航栏,它工作正常。我唯一的问题是,在移动设备上时,我希望下拉列表仅按预期的方式下拉。但是在计算机上,我希望它落在悬停上(它已经这样做了),但是如果单击它们,将它们发送到链接。我当前的困境是在手机或平板电脑上,他们单击下拉菜单时会将其发送到链接,而不是激活下拉菜单。我对Javascript不太熟悉,所以我不确定这是否是答案,但是代码笔是https://codepen.io/Religion/pen/BajmQMz

这是我的代码

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;200;300;400;500;600;700;800;900&display=swap');

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

body{
    font-family: 'Poppins', sans-serif;
    overflow: hidden;
}

ul{
    list-style: none;
}

a{
    text-decoration: none;
}

header{
    position: sticky;
    top: 0px;
    background-color: #60b4df;
    width: 100%;
    z-index: 1000;
}

section{
    position: relative;
    height: calc(100vh - 3rem);
    width: 100%;
    background: url('bg.jpg') no-repeat top center / cover;
    overflow: hidden;
}

.overlay{
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background-color: rgb(56, 165, 238, 0.5);
}

.container{
    max-width: 65rem;
    padding: 0 2rem;
    margin: 0 auto;
    display: flex;
    position: relative;
}

.logo-container{
    flex: 1;
    display: flex;
    align-items: center;
}

.nav-btn{
    flex: 3;
    display: flex;
}

.nav-links{
    flex: 2;
}

.log-sign{
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
}

.logo{
    color: #fff;
    font-size: 1.1rem;
    font-weight: 600;
    letter-spacing: 2px;
    text-transform: uppercase;
    line-height: 3rem;
}

.logo span{
    font-weight: 300;
}

.btn{
    display: inline-block;
    padding: .5rem 1.3rem;
    font-size: .8rem;
    border: 2px solid #fff;
    border-radius: 2rem;
    line-height: 1;
    margin: 0 .2rem;
    transition: .3s;
    text-transform: uppercase;
}

.btn.solid, .btn.transparent:hover{
    background-color: #fff;
    color: #69bde7;
}

.btn.transparent, .btn.solid:hover{
    background-color: transparent;
    color: #fff;
}

.nav-links > ul{
    display: flex;
    justify-content: center;
    align-items: center;
}

.nav-link{
    position: relative;
}

.nav-link > a{
    line-height: 3rem;
    color: #fff;
    padding: 0 .8rem;
    letter-spacing: 1px;
    font-size: .95rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    transition: .5s;
}

.nav-link > a > i{
    margin-left: .2rem;
}

.nav-link:hover > a{
    transform: scale(1.1);
}

.dropdown{
    position: absolute;
    top: 100%;
    left: 0;
    width: 10rem;
    transform: translateY(10px);
    opacity: 0;
    pointer-events: none;
    transition: .5s;
}

.dropdown ul{
    position: relative;
}

.dropdown-link > a{
    display: flex;
    background-color: #fff;
    color: #3498db;
    padding: .5rem 1rem;
    font-size: .9rem;
    align-items: center;
    justify-content: space-between;
    transition: .3s;
}

.dropdown-link:hover > a{
    background-color: #3498db;
    color: #fff;
}

.dropdown-link:not(:nth-last-child(2)){
    border-bottom: 1px solid #efefef;
}

.dropdown-link i{
    transform: rotate(-90deg);
}

.arrow{
    position: absolute;
    width: 11px;
    height: 11px;
    top: -5.5px;
    left: 32px;
    background-color: #fff;
    transform: rotate(45deg);
    cursor: pointer;
    transition: .3s;
    z-index: -1;
}

.dropdown-link:first-child:hover ~ .arrow{
    background-color: #3498db;
}

.dropdown-link{
    position: relative;
}

.dropdown.second{
    top: 0;
    left: 100%;
    padding-left: .8rem;
    cursor: pointer;
    transform: translateX(10px);
}

.dropdown.second .arrow{
    top: 10px;
    left: -5.5px;
}

.nav-link:hover > .dropdown,
.dropdown-link:hover > .dropdown{
    transform: translate(0, 0);
    opacity: 1;
    pointer-events: auto;
}

.hamburger-menu-container{
    flex: 1;
    display: none;
    align-items: center;
    justify-content: flex-end;
}

.hamburger-menu{
    width: 2.5rem;
    height: 2.5rem;
    display: flex;
    align-items: center;
    justify-content: flex-end;
}

.hamburger-menu div{
    width: 1.6rem;
    height: 3px;
    border-radius: 3px;
    background-color: #fff;
    position: relative;
    z-index: 1001;
    transition: .5s;
}

.hamburger-menu div:before,
.hamburger-menu div:after{
    content: '';
    position: absolute;
    width: inherit;
    height: inherit;
    background-color: #fff;
    border-radius: 3px;
    transition: .5s;
}

.hamburger-menu div:before{
    transform: translateY(-7px);
}

.hamburger-menu div:after{
    transform: translateY(7px);
}

#check{
    position: absolute;
    top: 50%;
    right: 1.5rem;
    transform: translateY(-50%);
    width: 2.5rem;
    height: 2.5rem;
    z-index: 90000;
    cursor: pointer;
    opacity: 0;
    display: none;
}

#check:checked ~ .hamburger-menu-container .hamburger-menu div{
    background-color: transparent;
}

#check:checked ~ .hamburger-menu-container .hamburger-menu div:before{
    transform: translateY(0) rotate(-45deg);
}

#check:checked ~ .hamburger-menu-container .hamburger-menu div:after{
    transform: translateY(0) rotate(45deg);
}

@keyframes animation{
    from{
        opacity: 0;
        transform: translateY(15px);
    }
    to{
        opacity: 1;
        transform: translateY(0px);
    }
}

@media (max-width: 920px){
    .hamburger-menu-container{
        display: flex;
    }

    #check{
        display: block;
    }

    .nav-btn{
        position: fixed;
        height: calc(100vh - 3rem);
        top: 3rem;
        left: 0;
        width: 100%;
        background-color: #69bde7;
        flex-direction: column;
        align-items: center;
        justify-content: space-between;
        overflow-x: hidden;
        overflow-y: auto;
        transform: translateX(100%);
        transition: .65s;
    }

    #check:checked ~ .nav-btn{
        transform: translateX(0);
    }

    #check:checked ~ .nav-btn .nav-link,
    #check:checked ~ .nav-btn .log-sign{
        animation: animation .5s ease forwards var(--i);
    }

    .nav-links{
        flex: initial;
        width: 100%;
    }

    .nav-links > ul{
        flex-direction: column;
    }

    .nav-link{
        width: 100%;
        opacity: 0;
        transform: translateY(15px);
    }

    .nav-link > a{
        line-height: 1;
        padding: 1.6rem 2rem;
    }

    .nav-link:hover > a{
        transform: scale(1);
        background-color: #50a9d6;
    }

    .dropdown, .dropdown.second{
        position: initial;
        top: initial;
        left: initial;
        transform: initial;
        opacity: 1;
        pointer-events: auto;
        width: 100%;
        padding: 0;
        background-color: #3183ac;
        display: none;
    }
    
    .nav-link:hover > .dropdown,
    .dropdown-link:hover > .dropdown{
        display: block;
    }

    .nav-link:hover > a > i,
    .dropdown-link:hover > a > i{
        transform: rotate(360deg);
    }

    .dropdown-link > a{
        background-color: transparent;
        color: #fff;
        padding: 1.2rem 2rem;
        line-height: 1;
    }

    .dropdown.second .dropdown-link > a{
        padding: 1.2rem 2rem 1.2rem 3rem;
    }

    .dropdown.second .dropdown.second .dropdown-link > a{
        padding: 1.2rem 2rem 1.2rem 4rem;
    }

    .dropdown-link:not(:nth-last-child(2)){
        border-bottom: none;
    }

    .arrow{
        z-index: 1;
        background-color: #69bde7;
        left: 10%;
        transform: scale(1.1) rotate(45deg);
        transition: .5s;
    }

    .nav-link:hover .arrow{
        background-color: #50a9d6;
    }

    .dropdown .dropdown .arrow{
        display: none;
    }

    .dropdown-link:hover > a{
        background-color: #3a91bd;
    }

    .dropdown-link:first-child:hover ~ .arrow{
        background-color: #50a9d6;
    }

    .nav-link > a > i{
        font-size: 1.1rem;
        transform: rotate(-90deg);
        transition: .7s;
    }

    .dropdown i{
        font-size: 1rem;
        transition: .7s;
    }

    .log-sign{
        flex: initial;
        width: 100%;
        padding: 1.5rem 1.9rem;
        justify-content: flex-start;
        opacity: 0;
        transform: translateY(15px);
    }
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://kit.fontawesome.com/64d58efce2.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="style.css">
    <title>Responsive Navbar</title>
</head>

<body>
  <header>
        <div class ="container-nav">
            <input type="checkbox" name="" id="check">
            
            <div class="logo-container">
                <h3 class="logo">David's<span> Dental</span></a></h3>
            </div>

            <div class="nav-btn">
                <div class="nav-links">
                    <ul>
                        <li class="nav-link" style="--i: .6s">
                            <a href="#">Home</a>
                        </li>
                        <li class="nav-link" style="--i: .85s">
                            <a href="{% url 'PatientInfo' %}">Patient Information<i class="fa fa-caret-down"></i></a>
                            <div class="dropdown">
                                <ul>
                                    <li class="dropdown-link">
                                        <a href="{% url 'visit' %}">First Dental Visit</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'NewPatient' %}">New Patients</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'checkup' %}">Check Ups</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'payment' %}">Insurance and Financial Information</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'COVID' %}">COVID-19 Office Re-opening</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'PatientReviews' %}">Patient Reviews</a>
                                    </li>

                                        
                                </ul>
                            </div>
                        </li>
                        <li class="nav-link" style="--i: 1.1s">
                            <a href="{% url 'service' %}">Services<i class="fa fa-caret-down"></i></a>
                            <div class="dropdown">
                                <ul>
                                    <li class="dropdown-link">
                                        <a href="{% url 'cosmeticdentistry' %}">Cosmetic Dentistry<i class="fa fa-caret-down"></i></a>
                                        <div class="dropdown second">
                                         <ul>
                                                <li class="dropdown-link">
                                                    <a href="{% url 'DentalBondings' %}">Dental Bonding</a>
                                                </li>
                                                <li class="dropdown-link">
                                                    <a href="{% url 'Clearbraces' %}">Clear Braces</a>
                                                </li>
                                                <li class="dropdown-link">
                                                    <a href="{% url 'Porcelainveneers' %}">Porcelain Veneers</a>
                                                </li>
                                                 <li class="dropdown-link">
                                                    <a href="{% url 'inlaysonlays' %}">Inlays & Onlays</a>
                                                </li>
                                                 <li class="dropdown-link">
                                                    <a href="{% url 'TeethWhitening' %}">Teeth Whitening</a>
                                                </li>
                                          </ul>
                                        </div>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'Restorativedentistry' %}">Restorative Dentistry<i class="fa fa-caret-down"></i></a>
                                        <div class="dropdown second">
                                         <ul>
                                                <li class="dropdown-link">
                                                    <a href="{% url 'DentalFillings' %}">Dental Fillings</a>
                                                </li>
                                                <li class="dropdown-link">
                                                    <a href="{% url 'DentalBridges' %}">Dental Bridges</a>
                                                </li>
                                                <li class="dropdown-link">
                                                    <a href="{% url 'DentalCrowns' %}">Dental Crowns</a>
                                                </li>
                                                 <li class="dropdown-link">
                                                    <a href="{% url 'Rootcanal' %}">Root Canal</a>
                                                </li>
                                                 <li class="dropdown-link">
                                                    <a href="{% url 'DentalImplants' %}">Dental Implants<i class="fa fa-caret-down"></i></a>
                                                    <div class="dropdown second">
                                                      <ul>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'Replacemissing' %}">Replace Missing Teeth</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'Overviewimplants' %}">Overview Of Implant Placement</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'missingupperor' %}">Missing all Upper or Lower Teeth</a>
                                                        </li>
                                                         <li class="dropdown-link">
                                                            <a href="{% url 'BoneGrafting' %}">Bone Grafting for Implants</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'Teeth_in_a_day' %}">Teeth-in-a-day</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'AfterImplantP' %}">After Implant Placement</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'CostDental' %}">Cost of Dental Implants</a>
                                                        </li>
                                                         <li class="dropdown-link">
                                                            <a href="{% url 'DentalFAQs' %}">
                                                            Dental Implants FAQs</a>
                                                        </li>
                                                      </ul>
                                                </li>
                                                 <li class="dropdown-link">
                                                    <a href="{% url 'Dentures' %}">Dentures<i class="fa fa-caret-down"></i></a>
                                                    <div class="dropdown second">
                                                      <ul>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'ImmediateDentures' %}">Immediate Dentures</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'PartialDentures' %}">Partial Dentures</a>
                                                        </li>
                                                        
                                                         
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'ExamsMaintenance' %}">Exams & Maintenance</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'DentalRelines' %}">Denture Relines</a>
                                                        </li>
                                                        <li class="dropdown-link">
                                                            <a href="{% url 'Softliners' %}">Soft Liners</a>
                                                        </li>
                                                         
                                                      </ul>
                                                </li>
                                          </ul>
                                        </div>
                                    </li>
                                            
                                    
                                </ul>
                            </div>
                        </li>
                        <li class="nav-link" style="--i: 1.35s">
                            <a href="{% url 'examcheckup' %}">Preventive Care<i class="fa fa-caret-down"></i></a>
                            <div class="dropdown">
                                <ul>
                                    <li class="dropdown-link">
                                        <a href="{% url 'examcheckup' %}">Dental Exams & Checkups</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'Hygiene' %}">Oral Hygiene</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'ChildDentistry' %}">Child Dentistry</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'Teethcleaning' %}">Teeth Cleaning</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'Teethcleaning' %}">Wisdom Teeth Removal</a>
                                    </li>
                                    <li class="dropdown-link">
                                        <a href="{% url 'Periodontal' %}">Gum Disease Treatment<i class="fa fa-caret-down"></i>
                                         <div class="dropdown second">
                                          <ul>
                                              <li class="dropdown-link">
                                                <a href="{% url 'Periodontal' %}">Periodontal Maintenance</a>
                                              </li>
                                              <li class="dropdown-link">
                                                  <a href="{% url 'Deepteeth' %}">Deep Teeth Cleaning</a>
                                              </li>
                                              </li>

                                             </ul>
                                              </div>
                                          </ul>
                            </div>

                        </li>
                            <li class="nav-link" style="--i: 1.35s">
                            <a href="{% url 'about' %}">About</a>
                        </li>
                        <li class="nav-link" style="--i: 1.35s">
                            <a href="{% url 'contact' %}">Contact</a>
                        </li>
                    </ul>
                </div>

                
            </div>

            <div class="hamburger-menu-container">
                <div class="hamburger-menu">
                    <div></div>
                </div>
            </div>
        </div>
    </header>
  </body

1 个答案:

答案 0 :(得分:0)

通常,当您单击某物时,它只会做一件事:

a)将它们发送到链接<a href="/link.html">link</a>

b)将显示下拉列表的Javascript。

不能同时做这两项,因为它不知道单击时的意思。

建议:链接旁边有一个箭头。箭头可展开或收缩菜单。文本是链接。

前 [HOME>]

HOME点击,进入链接

点击

>,展开菜单。

由于菜单可能会因屏幕尺寸而异,因此您可能需要@media设置。

https://www.w3schools.com/cssref/css3_pr_mediaquery.asp