简体   繁体   English

如何让我的导航栏完全响应?

[英]How can I make my navbar completely responsive?

I recently made an Apple navbar, but I have some trouble with getting it to be responsive.我最近制作了一个 Apple 导航栏,但我在让它响应时遇到了一些麻烦。 https://codepen.io/51LV3R/pen/OJRxNdj is link for the navbar. https://codepen.io/51LV3R/pen/OJRxNdj是导航栏的链接。 The searchbar which is supposed to be in the center transforms after I close it on a mobile device.当我在移动设备上关闭它后,应该位于中心的搜索栏会发生变化。 Does anyone know how can I fix that?有谁知道我该如何解决? As you can see at the bottom of the HTML code, I tried using jQuery. Also, can I add HTML elements through DOM wherever I want?正如您在HTML代码底部看到的那样,我尝试使用jQuery。另外,我可以通过DOM在任何地方添加HTML元素吗? Because I want on click to searchbar to show the button 'cancel' much like the Apple one does.因为我想点击搜索栏以显示“取消”按钮,就像 Apple 那样。 Thanks for help.感谢帮助。 https://www.apple.com/ https://www.apple.com/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cubing Website</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <div class="container">
            <nav class="nav">
                <ul class="nav-list nav-list-mobile">
                    <li class="nav-item">
                        <div class="mobile-menu">
                            <span class="line line-top"></span>
                            <span class="line line-bottom"></span>
                        </div>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link nav-link-apple" onmouseover="hover();"></a>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link nav-link-bag" onmouseover="hover();"></a>
                    </li>
                </ul>
                <!-- nav list nav mobile -->
                <ul class="nav-list nav-list-larger">
                    <li class="nav-item nav-item-hidden">
                        <a href="index.html" class="nav-link nav-link-apple n1" onmouseover="hover();"></a>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link n2" onmouseover="hover();">Mac</a>
                    </li>
                    <li class=" nav-item">
                        <a href="#" class="nav-link n3" onmouseover="hover();">iPad</a>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link n4" onmouseover="hover();">iPhone</a>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link n5" onmouseover="hover();">Watch</a>
                    </li>
                    <li class="nav-item" >
                        <a href="#" class="nav-link n6" onmouseover="hover();">TV</a>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link n7" onmouseover="hover();">Music</a>
                    </li>
                    <li class="nav-item">
                        <a href="#" class="nav-link n8" onmouseover="hover();">Support</a>
                    </li>
                    <li class="nav-item">
                        <a href="#" id="search" onclick="navbar();" class="nav-link nav-link-search n9" onmouseover="hover();"></a>
                    </li>
                    <li class="nav-item v2">
                        <a href="#" class="nav-link nav-link-bag n10" onmouseover="hover();"></a>
                    </li>
                    <li class="searchbox" id="searchbox">
                        <form>
                        <button class="confirm" disabled></button>
                        <input  type="text" class="search" id="box" placeholder="Search here" autocorrect="off" autocapitalize="off" autocomplete="off" spellcheck="false">
                        <span class="close-btn" onclick="navbarRemove();">
                            <span class="close-btn-left"></span>
                            <span class="close-btn-right"></span>
                        </span>
                        </form>
                    </li>
                </ul>
                <!-- nav list nav mobile -->
            </nav>
        </div>
    </header>
<script src="main.js"></script>
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        if ($('html').width() <= 767) {
        $('#search').click(function(){
            $('.nav-link').addClass('hide-item')
            $('.searchbox').addClass('active')
            $('.nav-link').removeClass('activev2')
        })
        $('.close-btn').click(function(){
            $('.nav-link').removeClass('hide-item')
            $('.searchbox').removeClass('active')
            $('.nav-link').addClass('activev2')
        })
    }else{
        
    }
    })
</script>
</body>
</html>
*,
*::before,
*::after{
    margin: 0;
    padding: 0;
}

html{
    font-size: 10px;
    font-family: 'SF Pro Display', sans-serif;
}

a{
    display: block;
    text-decoration: none;
}

.container{
    max-width: 98rem;
    margin: 0 auto;
    padding: 0 2.2rem;
}

header{
    position: fixed;
    top: 0;
    z-index: 1400;
    width: 100%;
    height: 4.4rem;
    background-color: rgba(0,0,0,.8);
    backdrop-filter: blur(2rem);
}

.nav-list{
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 -0.5rem;
}

.nav-list-mobile{
    display: none;
}

.nav-link{
    font-size: 1.4rem;
    color: #e4e1e1;
    padding: 0 1rem;
    transition: opacity .5s;
}

.nav-link:hover{
    opacity: .7;
    transition: .3s!important;
}

.nav-link.hide-item{
    transform: scale(0);
    transition: .5s;
}

.nav-link.activev2{
    transition: .5s;
    transform: scale(1);
}

.nav-link.activev2:hover{
    transition: .3s!important;
}

.n1.activev2{
    transition-delay: 0.0005s!important;
}

.n2.activev2{
    transition-delay: 0.0025s!important;
}

.n3.activev2{
    transition-delay: 0.005s!important;
}

.n4.activev2{
    transition-delay: 0.025!important;
}

.n5.activev2{
    transition-delay: 0.05s!important;
}

.n6.activev2{
    transition-delay: 0.1s!important;
}

.n7.activev2{
    transition-delay: 0.15s!important;
}

.n8.activev2{
    transition-delay: 0.2s!important;
}

.n9.activev2{
    transition-delay: 0.25s!important;
}

.n10.activev2{
    transition-delay: 0.3s!important;
}

.n10{
    transition-delay: 0.0005s!important;
}

.n9{
    transition-delay: 0.0025s!important;
}

.n8{
    transition-delay: 0.005s!important;
}

.n7{
    transition-delay: 0.025!important;
}

.n6{
    transition-delay: 0.05s!important;
}

.n5{
    transition-delay: 0.1s!important;
}

.n4{
    transition-delay: 0.15s!important;
}

.n3{
    transition-delay: 0.2s!important;
}

.n2{
    transition-delay: 0.25s!important;
}

.n1{
    transition-delay: 0.3s!important;
}

.nav-link-apple{
    width: 1.6rem;
    height: 3.5rem;
    background: url("img/apple.svg") center no-repeat;
}

.nav-link-search{
    width: 1.6rem;
    height: 4.4rem;
    background: url("img/search.svg") center no-repeat;
    font-size: 1.4rem;
    color: #e4e1e1;
    padding: 0 1rem;
    transition: opacity .5s;
    }
    
    .nav-link-search:hover{
        opacity: .7;
    }

.nav-link-bag{
    width: 1.4rem;
    height: 4.4rem;
    background: url("img/bag.svg") center no-repeat;
}

.searchbox{
    position: absolute;
    top: 0;
    left: 50%;
    transform: translate(-47%, -45%);
    width: 600px;
    height: 44px;
    opacity: 0;
    visibility: hidden;
    transition: .6s;
}

.searchbox.active{
    opacity: 1;
    visibility: visible;
    transition-delay: .6s;
    transform: translate(-50%, -45%);
}

.searchbox input{
    width: 100%;
    height: 44px;
    background: transparent;
    outline: none;
    border: none;
    color: #fff;
    font-size: 18px;
}

.searchbox input:focus{
    outline: none;
    border: none;
}

.close-btn{
    opacity: .7;
    transition: .5s;
}

.close-btn-left{
    position: absolute;
    margin-top: 2rem;
    transform: rotate(45deg);
    width: 1.7rem;
    height: 1px;
    background-color: #979696;
    cursor: pointer;
}

.close-btn-right{
    position: absolute;
    margin-top: 2rem;
    transform: rotate(-45deg);
     width: 1.7rem;
     height: 1px;
     background-color: #979696;
     cursor: pointer;
}

.close-btn:hover{
    opacity: 1.6;
}

.confirm{
    width: 18px;
    height: 18px;
    background: url("img/idkv2.svg") center no-repeat;
    font-size: 1.4rem;
    fill: #e4e1e1;
    border: none;
    outline: none;
    transform: translate(-160%, 180%);
}

@media screen and (max-width: 767px){
    header{
        height: 4.8rem;
        transition: background .36s cubic-bezier(0.32, 0.08, 0.24, 1),
                               height .56s cubic-bezier(0.32, 0.08, 0.24, 1);
    }

    header .container{
        padding: 0;
    }

    .nav-list{
        margin-top: 0;
    }

    .nav-list-mobile{
        display: flex;
    }

    .nav-item{
        width: 4.8rem;
        height: 4.8rem;
        display: flex;
        justify-content: center;
    }

    .nav-item-hidden{
        display: none;
    }

    .mobile-menu{
        position: relative;
        z-index: 1500;
        width: 100%;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        cursor: pointer;
    }

    .v2{
        display: none;
    }

    .line{
        position: absolute;
        width: 1.7rem;
        height: 1px;
        background-color: #fff;
        transition: margin-top .3192s cubic-bezier(0.04, 0.04, 0.12, 0.96);
    }

    .line-top{
        margin-top: 4px;
    }

    .line-bottom{
        margin-top: -.8rem;
    }

    .active .line-top{
        margin-top: 0;
        transform: rotate(45deg);
        transition: transform .3192s .1s cubic-bezier(0.04, 0.04, 0.12, 0.96);
    }

    .active .line-bottom{
        margin-top: 0;
        transform: rotate(-45deg);
        transition: transform .3192s .1s cubic-bezier(0.04, 0.04, 0.12, 0.96);
    }

    header.active{
        height: 100%;
        background-color: #000;
    }

    .nav-link-apple{
        width: 1.8rem;
        height: 4.8rem;
        position: relative;
        z-index: 1500;
    }

    .nav-link-bag{
        width: 1.6rem;
        height: 4.8rem;
        transition: opacity 1.5s;
    }

    .nav{
        position: relative;
    }

    .nav-link{
        font-size: 1.7rem;
        padding: 0;
        margin: auto 0;
    }

    .nav-list-larger{
          position: fixed;
          top: 0;
          left: 0;
          width: 0;
          height: 0;
          display: block;
          padding: 10.5rem 5rem;
          z-index: 1000;
          box-sizing: border-box;
          opacity: 0;
          visibility: hidden;
          transition: opacity .3s;
    }

    .active .nav-list-larger{
        width: 100%;
        height: 100vh;
        opacity: 1;
        visibility: visible;
    }

    .active .nav-link-bag{
        opacity: 0;
        transition: opacity .3s;
    }

    .nav-list-larger .nav-item{
        width: 100%;
        justify-content: flex-start;
        border-bottom: 2px solid rgba(255,255,255,.1);
    }

    .nav-list-larger .nav-item:nth-child(9){
        border-bottom: none;
    }

    .active .nav-list-larger .nav-item{
        animation: fadeIn .5s ease-in-out;
    }

    .close-btn{
        display: none;
    }

    .n9{
        display: none;
    }

    .searchbox{
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 8rem;
        opacity: 1;
        visibility: visible;
        background: #1D1D1F;
        height: 40px;
        border-radius: 4px;
        width: 400px;
        transition: none;
    }

    .searchbox-active{
        display: none;
        opacity: 0;
        visibility: none;
        transform: translate(0)!important;
        transition: none;
    }

    @keyframes fadeIn {
        from{
            opacity: 0;
        }
        to{
            opacity: 1;
        }
    }
}

 @keyframes appear {
     from{
         transform: scale(0);
     }
     to{
         transform: scale(1);
     }
 }
const selectElement = (element) => document.querySelector(element);

selectElement('.mobile-menu').addEventListener('click', () => {
    selectElement('header').classList.toggle('active');
});

function navbar(){
    document.querySelector('html').style.transition = ".2s"
    document.querySelector('html').style.background = "rgba(0,0,0,0.5)"
    setTimeout(()=> {
        document.querySelector('#box').focus()
    }, 1000)
}

function navbarRemove(){
    document.querySelector('html').style.transition = "0"
    document.querySelector('html').style.background = "none"
    document.getElementById("nav-link").style.transform = "scale(1)";
}

Like I said the problem was in transitions.就像我说的那样,问题出在过渡中。 Remove the transition option of.6s from.searchbox in general css and add 1s transition in media queries where max-width: 767px.一般css去掉.6s from.searchbox的transition选项,在max-width: 767px的media queries中添加1s transition。 See the codepen: https://codepen.io/codeweapon/pen/oNzGLZB参见codepen: https://codepen.io/codeweapon/pen/oNzGLZB

.searchbox{
    position: absolute;
    top: 0;
    left: 50%;
    transform: translate(-47%, -45%);
    width: 600px;
    height: 44px;
    opacity: 0;
    visibility: hidden;
/*     transition: .6s; */
}

@media screen and (max-width: 767px){
.searchbox{
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 8rem;
        opacity: 1;
        visibility: visible;
        background: #1D1D1F;
        height: 40px;
        border-radius: 4px;
        width: 400px;
        transition: 1s; /*add transition of 1s */
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM