Home > Software engineering >  Block User scroll when full screen menu is open
Block User scroll when full screen menu is open

Time:01-05

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.

Here is my code at the moment:

<body id="body">
    <div >
        <div >
            <div >
                <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 >MENU</p>
                <img src="images/close-line.png"  onclick="closemenu()">
            </nav>
            <img src="images/hamburger-menu.png"  onclick="openmenu()">
        </div>
    </div> 

    <section >
        <h2>Hello World</h2>
    </section>
    <section ></section>
    <section ></section>
    <section ></section>
    <section ></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

        *{
    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!

CodePudding user response:

I used document.querySelector('body').classList.add('no-scroll') when menu opened & document.querySelector('body').classList.remove('no-scroll') when menu closed.

        *{
    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 >
        <div >
            <div >
                <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 >MENU</p>
                <img src="images/close-line.png"  onclick="closemenu()">
            </nav>
            <img src="images/hamburger-menu.png"  onclick="openmenu()">
        </div>
    </div> 

    <section >
        <h2>Hello World</h2>
    </section>
    <section ></section>
    <section ></section>
    <section ></section>
    <section ></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>

  •  Tags:  
  • Related