简体   繁体   English

全屏菜单打开时阻止用户滚动

[英]Block User scroll when full screen menu is open

i'm working on a full screen menu for a website but I need to disable the user to scroll while the menu is open.我正在为网站制作全屏菜单,但我需要在菜单打开时禁用用户滚动。 I can't find a fitting solution on the internet so it would be great if anyone could help me.我在互联网上找不到合适的解决方案,所以如果有人可以帮助我,那就太好了。 I am not sure how to trigger the body to no-scroll if the menu is open.如果菜单打开,我不确定如何触发正文不滚动。 I am not that familiar with js.我对js不是很熟悉。

Here is my code at the moment:这是我目前的代码:

<body id="body">
    <div class="navbar">
        <div class="navbar-wrapper">
            <div class="logo">
                <img src="images/Gautama_Buddha_pic.png">
            </div>
            <nav id="menu">
                <ul>
                    <li><a href="">ARTIST</a></li>         
                    <li><a href="">EXHIBITIONS</a></li>         
                    <li><a href="">EVENTS</a></li>         
                    <li><a href="">VISIT US</a></li>         
                </ul>
                <p class="lite-text">MENU</p>
                <img src="images/close-line.png" class="close-icon" onclick="closemenu()">
            </nav>
            <img src="images/hamburger-menu.png" class="menu-icon" onclick="openmenu()">
        </div>
    </div> 

    <section class="one">
        <h2>Hello World</h2>
    </section>
    <section class="two"></section>
    <section class="three"></section>
    <section class="four"></section>
    <section class="five"></section>
    
<script>
    var menu = document.getElementById("menu");

    function closemenu(){

        menu.style.top = "-100vh";
    }

    function openmenu(){

        menu.style.top = "0";
    }
</script>

</body>

And that's the CSS这就是 CSS

        *{
    padding: 0;
    margin: 0;
    font-family: sans-serif;
    user-select: none;
}

.container{
    height: 100vh;
    width: 100%;
    background-color: white;
    position: relative;
}

.navbar{
    width: 100%;
    position: fixed;
}

.navbar-wrapper{
    width: 90%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: auto;
}

.logo img{
    width: 50px;
    cursor: pointer;
    margin: 35px 0;
}

nav ul li{
    list-style: none;
    margin: 35px 0;
}

nav ul li a{
    text-decoration: none;
    font-size: 40px;
    color: white;
    padding: 10px;
    letter-spacing: 5px;
    position: relative;
}

nav ul li a::after{
    content: '';
    height: 3px;
    width: 0%;
    background: #dfa24e;
    position: absolute;
    bottom: 0;
    left: 0;
    transition: width 0.5s;
}

nav ul li a:hover::after{
    width: 100%;
}

nav{
    position: absolute;
    width: 100%;
    height: 100vh;
    background-color: grey;
    z-index: 2;
    top: -100vh;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    transition: 1s;
    overflow: hidden;
}

.lite-text{
    color: transparent;
    font-size: 200px;
    letter-spacing: 100px;
    opacity: 0.1;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-weight: 800;
    z-index: -1;
    -webkit-text-stroke: 5px #000;
}

.close-icon{
    width: 25px;
    position: absolute;
    right: 80px;
    top: 50px;
    cursor: pointer;
}

.menu-icon{
    width: 30px;
    cursor: pointer;
}

section{
    height: 100vh;
    width: 100%;
}

.one{
    background-color: tomato;
}
.two{
    background-color: thistle;
}
.three{
    background-color: blue;
}
.four{
    background-color: blueviolet;
}
.five{
    background-color: wheat;
}

    .no-scroll {
    overflow:hidden;
}

Thanks for your help!谢谢你的帮助!

I used document.querySelector('body').classList.add('no-scroll') when menu opened & document.querySelector('body').classList.remove('no-scroll') when menu closed.我在菜单打开时使用document.querySelector('body').classList.add('no-scroll') document.querySelector('body').classList.remove('no-scroll')

 *{ padding: 0; margin: 0; font-family: sans-serif; user-select: none; }.container{ height: 100vh; width: 100%; background-color: white; position: relative; }.navbar{ width: 100%; position: fixed; }.navbar-wrapper{ width: 90%; display: flex; align-items: center; justify-content: space-between; margin: auto; }.logo img{ width: 50px; cursor: pointer; margin: 35px 0; } nav ul li{ list-style: none; margin: 35px 0; } nav ul li a{ text-decoration: none; font-size: 40px; color: white; padding: 10px; letter-spacing: 5px; position: relative; } nav ul li a::after{ content: ''; height: 3px; width: 0%; background: #dfa24e; position: absolute; bottom: 0; left: 0; transition: width 0.5s; } nav ul li a:hover::after{ width: 100%; } nav{ position: absolute; width: 100%; height: 100vh; background-color: grey; z-index: 2; top: -100vh; left: 0; display: flex; align-items: center; justify-content: center; text-align: center; transition: 1s; overflow: hidden; }.lite-text{ color: transparent; font-size: 200px; letter-spacing: 100px; opacity: 0.1; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); font-weight: 800; z-index: -1; -webkit-text-stroke: 5px #000; }.close-icon{ width: 25px; position: absolute; right: 80px; top: 50px; cursor: pointer; }.menu-icon{ width: 30px; cursor: pointer; } section{ height: 100vh; width: 100%; }.one{ background-color: tomato; }.two{ background-color: thistle; }.three{ background-color: blue; }.four{ background-color: blueviolet; }.five{ background-color: wheat; }.no-scroll { overflow:hidden; }
 <body id="body"> <div class="navbar"> <div class="navbar-wrapper"> <div class="logo"> <img src="images/Gautama_Buddha_pic.png"> </div> <nav id="menu"> <ul> <li><a href="">ARTIST</a></li> <li><a href="">EXHIBITIONS</a></li> <li><a href="">EVENTS</a></li> <li><a href="">VISIT US</a></li> </ul> <p class="lite-text">MENU</p> <img src="images/close-line.png" class="close-icon" onclick="closemenu()"> </nav> <img src="images/hamburger-menu.png" class="menu-icon" onclick="openmenu()"> </div> </div> <section class="one"> <h2>Hello World</h2> </section> <section class="two"></section> <section class="three"></section> <section class="four"></section> <section class="five"></section> <script> var menu = document.getElementById("menu"); function closemenu(){ document.querySelector('body').classList.remove('no-scroll') menu.style.top = "-100vh"; } function openmenu(){ document.querySelector('body').classList.add('no-scroll') menu.style.top = "0"; } </script> </body>

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

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