首页 > 解决方案 > 当我向下滚动时如何使导航菜单消失

问题描述

我的网站上有以下导航栏。(在最大宽度 540px 的屏幕上)

当我在我的页面上向下滚动时,导航栏保持打开状态,我希望它在滚动时消失。(将其翻译回 -100% )就像在切换汉堡线之前一样。

我假设我需要在我的 js 文件中进行更多验证。

欢迎任何帮助,在此先感谢您。

您可以在此处查看代码:https ://codepen.io/bunea-andrei/pen/jOBGNgd?editors=1010 。

或者这个片段:

我不确定您是否可以调整片段窗口的大小以查看我在说什么。

const wrapperSlide = () => {
  const burger = document.querySelector(".burger");
  const wrapper = document.querySelector(".wrapper");

  const wrapperLinks = document.querySelectorAll(".wrapper span");

  burger.addEventListener("click", () => {

    wrapper.classList.toggle("wrapper-active");


    window.addEventListener('mouseup', function(event) {
      if (event.target != wrapper && event.target != burger && event.target.parentNode != burger && event.target.parentNode != wrapper && burger.classList.contains("toggle")) {
        wrapper.classList.remove("wrapper-active");
        burger.classList.toggle("toggle");
      }
    });



    //Burger Animation
    burger.classList.toggle("toggle");
  });

}


wrapperSlide();
/*
html5doctor.com Reset Stylesheet
v1.6.1
Last Updated: 2010-09-17
Author: Richard Clark - http://richclarkdesign.com
Twitter: @rich_clark
*/

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-size: 100%;
    vertical-align: baseline;
    background: transparent;
}

body {
    line-height: 1;
}

article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
    display: block;
}

    nav ul {
        list-style: none;
    }

blockquote, q {
    quotes: none;
}

    blockquote:before, blockquote:after,
    q:before, q:after {
        content: '';
        content: none;
    }

a {
    margin: 0;
    padding: 0;
    font-size: 100%;
    vertical-align: baseline;
    background: transparent;
}

/* change colours to suit your needs */
ins {
    background-color: #ff9;
    color: #000;
    text-decoration: none;
}

/* change colours to suit your needs */
mark {
    background-color: #ff9;
    color: #000;
    font-style: italic;
    font-weight: bold;
}

del {
    text-decoration: line-through;
}

abbr[title], dfn[title] {
    border-bottom: 1px dotted;
    cursor: help;
}

table {
    border-collapse: collapse;
    border-spacing: 0;
}

/* change border colour to suit your needs */
hr {
    display: block;
    height: 1px;
    border: 0;
    border-top: 1px solid #cccccc;
    margin: 1em 0;
    padding: 0;
}

input, select {
    vertical-align: middle;
}






menu {
    width: 100%;
    background: black;
    height: 90px;
    border-bottom: 22px solid red;
    
    display: flex;
    justify-content: center;
    align-items: center;
}



 menu .box { /* creating the box in which i will put each item of the nav menu*/
        min-width: 180px; 
        height: 70px;
        text-align: center;
        line-height: 65px;
        cursor: pointer;
    }


 menu span {    /*  editing the writing of the menu bar*/
        color: white;
        font-size: 25px; 
         margin-top: 30px;  
    }


  menu .burger div {
        width: 35px;
        height: 4px;
        display: none;
        background-color: #97903f;
        transition: all 0.3s ease;      
    }


    menu .burger .line1, .line2 {   /* margins only for first and second line*/
        margin-bottom: 5px;
    }



@media only screen and (max-width: 540px) { /*    IPHONE 6/7/8/10   SAMSUNG GALAXY S5  and some small tablets */


    /* --------------  menu with navigation    ------------------   */

    menu {
      top:0;
        border-bottom: none;
        height: 40px;
        background-color: rgb(35 22 68 / 0.94);
        justify-content: left;
        width: 100%;
        position: fixed;
        z-index: 100; /* to make it pass the image slider and everything when scrolling */
        /* to make it follow the user when he scrolls*/
    }


        menu .burger {
            margin-left: 10px;
        }


            menu .burger div { /* editing the burger lines */
                display: block;
            }


        menu .wrapper {
            top: 40px;
            position: absolute;
            left: 0;
            width: 200px;
            height: 498px;
            display: inline-block;
            background-color: rebeccapurple;
            transform: translateX(-102%); /* why 102 wtf g */
            transition: transform 0.5s ease-in;
        }

        menu .line { /* modify the separation line from vertical display to horizontal */
            height: 1px;
            width: 100%;
            background: grey;
        }


    .wrapper.wrapper-active { /*  bringing the nav bar up when the user clicks the burger lines */
        transform: translateX(0%);
    }


    /*  animating the burger lines in the shape of an x */

    .toggle .line1 {
        transform: rotate(-45deg) translate(-5.5px, 7px);
    }

    .toggle .line2 {
        opacity: 0;
    }

    .toggle .line3 {
        transform: rotate(45deg) translate(-5.5px, -7px);
    }


  content{
    display:block;
    widht:300px;
    height:auto;
    text-align:center;
    line-height:1.5em;
    margin-top:50px;
  }

}
    
        
    
<menu>

  <div class="burger">
    <div class="line1"></div>
    <div class="line2"></div>
    <div class="line3"></div>
  </div>


  <div class="wrapper">

    <div class="line"></div>
    <div onclick="location.href='https://www.google.com/';" class="box">
      <span>Fruits</span>
    </div>

    <div class="line"></div>
    <div onclick="location.href='https://www.google.com/';" class="box">
      <span>Garbage</span>
    </div>

    <div class="line"></div>
    <div onclick="location.href='https://www.google.com/';" class="box">
      <span>Flower</span>
    </div>

    <div class="line"></div>
    <div onclick="location.href='https://www.google.com/';" class="box">
      <span>Books</span>
    </div>

    <div class="line"></div>
    <div onclick="location.href='https://www.google.com/';" class="box">
      <span>Cakes</span>
    </div>

    <div class="line"></div>
    <div onclick="location.href='https://www.google.com/';" class="box">
      <span>Just Stuff</span>
    </div>

    <div class="line"></div>
    <div onclick="location.href='https://www.google.com/';" class="box">
      <span>Hello</span>
    </div>
    <div class="line"></div>

  </div>

</menu>

<content>
  <p>Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College
    in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33
    of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes
    from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original
    form, accompanied by English versions from the 1914 translation by H. Rackham. Where can I get some? There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised
    words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined
    chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum
    is therefore always free from repetition, injected humour, or non-characteristic words etc. What is Lorem Ipsum? 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. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised
    in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</content>

标签: javascripthtmlcss

解决方案


推荐阅读