Prevent user scrolling when full-screen menu is activated

Currently, I am developing a full-screen menu for a website and facing an issue with disabling the user's ability to scroll when the menu is open. Despite searching online, I have not found a suitable solution. It would be greatly appreciated if someone could provide assistance as I am unsure how to prevent scrolling in the body element when the menu is active, particularly since my JavaScript knowledge is limited.

Below is the code snippet I am currently working on:

<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>

Here's the accompanying 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;
}
...
... (CSS continuation) 
...
    .no-scroll {
    overflow:hidden;
}

Your help is highly valued! Thank you!

Answer №1

To enable scrolling when the menu is opened, I utilized the following code:

document.querySelector('body').classList.add('no-scroll')
. This code removes the ability to scroll when the menu is closed:
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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using $stateParams injection for unit testing Angular applications with Karma

Here is the starting point for the controller code: angular .module('hc.hotelContent') .controller('NameLocationController', nameLocationCtrlFn); //Todo change hotelDataService nameLocationCtrlFn.$inject = ['$stateParams', &a ...

A challenge in web design: tackling cross-browser color discrepancies

I have implemented CSS code in an HTML page to define the color values of H1 and H2 elements. <style type="text/css"> img {border-style: none;} H1 {color: "#33CCFF"} H2 {color: "#33CCFF"} </style> While this setup works well on Internet Explo ...

Enhancing the level of abstraction in selectors within Redux using TypeScript

Here is a custom implementation of using Redux with TypeScript and the connect method. import { connect, ConnectedProps } from 'react-redux' interface RootState { isOn: boolean } const mapState = (state: RootState) =&g ...

The nested navigation bar is not displaying properly below the main navigation bar

Hey there! I'm having a bit of trouble with adding a nested nav within my main nav. The issue is that the nested nav isn't aligning properly under the main nav. Take a look at this screenshot for reference. The nested nav seems to be shifted abou ...

Updated the object in an array retrieved from an API by adding a new key-value pair. Attempted to toggle the key value afterwards, only to find that

Essentially, I am retrieving products from an API and including a new key value isAdded in the object within the products array. I utilized a foreach loop to add that key and it was successfully added. Now, when I click the Add to cart button, the product ...

Dynamically showcasing content in an HTML table

Having trouble with this code snippet. It's supposed to iterate through array objects and display them in an HTML table. The third row should have buttons, but nothing is showing up. Can you spot the issue? Here's the HTML code: <html> & ...

Exploring the Structure of Trees using JavaScript

Here are my terms: var terms = [ { id: 1, name: "Name 1", parent: null }, { id: 2, name: "Name 2", parent: 6 }, { id: 3, name: "Name 3", parent: null }, { id: 4, name: "Name 4", parent: 2}, { id: 5, name: "Name 5", ...

The timestamp will display a different date and time on the local system if it is generated using Go on AWS

My angular application is connected to a REST API built with golang. I have implemented a todo list feature where users can create todos for weekly or monthly tasks. When creating a todo, I use JavaScript to generate the first timestamp and submit it to th ...

Customizing the appearance of two separate tables

I have a pair of tables that I want to style differently. Table 1 needs the images centered with no border, while table 2 should have text left-aligned with a border. Here is the CSS: table, th, td { border: 1px solid #999; border-collapse: colla ...

Implementing a custom body class in AngularJS when utilizing partials

Looking for some help with AngularJS. I have an index.html file, along with controllers and partials. The <body> tag is located in the index.html. I am trying to set the class for the body using my controller. After adding a value to $scope.body_c ...

Navigating through object keys in YupTrying to iterate through the keys of an

Looking for the best approach to iterate through dynamically created forms using Yup? In my application, users can add an infinite number of small forms that only ask for a client's name (required), surname, and age. I have used Formik to create them ...

What could be causing mobile phones to overlook my CSS media query?

My CSS includes 3 media queries that function properly when I resize the browser, but fail to work when using the responsive design tool in the inspector ("toggle device mode") and on mobile devices. Snippet of my CSS code : @media screen and (max-width: ...

Update the text for the filter search placeholder in the Ant Table component

Is there a way to alter the default placeholder text in the Ant Table? I've set up a functioning example in documentation but couldn't find any prop for customization besides the customized filter dropdown, which I didn't want to implement. ...

Asynchronous requests in Node.js within an Array.forEach loop not finishing execution prior to writing a JSON file

I have developed a web scraping Node.js application that extracts job description text from multiple URLs. Currently, I am working with an array of job objects called jobObj. The code iterates through each URL, sends a request for HTML content, uses the Ch ...

Tips for keeping your avatar contained within a Bootstrap grid

I am attempting to adjust the positioning of my "image holder" (avatar) with a top and bottom margin of 3px, but when I implement this change, the image holder shifts outside of the grid. Below is the code snippet in question: <div class="container con ...

What is the best way to incorporate text animation into a website using Cascading Style Sheets (CSS)?

Is there a way to make my text flash twice per second? I attempted to create animated text, but it didn't work out! ...

Transform the MUI Typescript Autocomplete component to output singular values of a specific property rather than a complete object

When utilizing MUI Autocomplete, the generic value specified in onChange / value is determined by the interface of the object set in the options property. For instance, consider an autocomplete with the following setup: <Autocomplete options={top ...

the order of initialization in angularjs directives with templateUrl

In my current scenario, I am faced with a situation where I need to broadcast an event from one controller and have another directive's controller receive the message. The problem arises because the event is sent immediately upon startup of the contro ...

iframe failing to load link after initial attempt

As a beginner in using iframes, I have come across an issue that is puzzling me. I have a navigation bar and an iframe on my webpage. When I click on the "Search" link, it loads into the iframe without any problems and I can work within it. However, if I c ...

Use jQuery to switch back and forth between the login and registration forms on a single

I've set up two forms, one for login and one for registration, with the default view showing the login form. There's a link that says "Don't have an account?" and when it's clicked, the registration form will display while the login for ...