简体   繁体   中英

How to stop page from scrolling in off-canvas menu?

 @import url("https://fonts.googleapis.com/css?family=Oswald:200,300,400,500,600,700&display=swap"); * { margin: 0px; padding: 0px; box-sizing: border-box; } body { font-family: "Oswald", sans-serif; height: 100%; overflow: hidden; } img { width: 100%; } /* Utility */ .container { margin: 0 auto; max-width: 1160px; padding: 0 20px; overflow: hidden; } .overlay { top: 0; left: 0; right: 0; bottom: 0; position: absolute; background-color: rgba(0, 0, 0, 0.5); } .hidden { display: none; } .dark { background: #000; } .active { opacity: 0.7; } .parent { position: relative; } /* Home */ .showcase { position: relative; height: 100vh; background-image: url(../img/homebg/bg1.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; animation: slide 18s infinite; transition: 100ms ease-in-out; width: 100%; } .showcase h1 { font-size: 3.5rem; font-weight: 500; color: #fff; padding-top: 40px; text-transform: uppercase; position: absolute; z-index: 1; } @keyframes slide { 0% { background-image: url(../img/homebg/bg1.jpg); } 20% { background-image: url(../img/homebg/bg2.jpg); } 40% { background-image: url(../img/homebg/bg3.jpg); } 60% { background-image: url(../img/homebg/bg4.jpg); } 80% { background-image: url(../img/homebg/bg5.jpg); } 100% { background-image: url(../img/homebg/bg1.jpg); } } /* Personal */ .pimg1 { background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("../img/homebg/bg1.jpg"); min-height: 500px; } .pimg2 { background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("../img/homebg/bg2.jpg"); min-height: 500px; } .ptext1 { color: #fff; display: flex; justify-content: center; align-items: center; padding-top: 300px; font-size: 3rem; font-weight: 500; text-transform: uppercase; } /* Hamburger menu */ .menu-wrap { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .menu-wrap .toggler { top: 55px; right: 280px; cursor: pointer; width: 50px; height: 50px; opacity: 0; position: absolute; z-index: 10; } .menu-wrap .hamburger { top: 55px; right: 270px; width: 70px; height: 60px; padding: 1rem; background: transparent; display: flex; align-items: center; justify-content: center; position: absolute; z-index: 1; } /* Hamburger Icon */ .menu-wrap .hamburger > div { position: relative; flex: none; width: 100%; height: 4px; background: #fff; display: flex; align-items: center; justify-content: center; transition: all 0.4s ease; } .menu-wrap .hamburger > div::before, .menu-wrap .hamburger > div::after { content: ""; position: absolute; z-index: 1; top: -10px; width: 100%; height: 4px; background: inherit; } .menu-wrap .hamburger > div::after { top: 10px; } /* Toggler Animation */ .menu-wrap .toggler:checked + .hamburger > div { transform: rotate(135deg); } .menu-wrap .toggler:checked + .hamburger > div:before, .menu-wrap .toggler:checked + .hamburger > div:after { top: 0; transform: rotate(90deg); } .menu-wrap .toggler:checked:hover + .hamburger > div { transform: rotate(225deg); } /* Show Menu */ .menu-wrap .toggler:checked { overflow: hidden; } .menu-wrap .toggler:checked ~ .menu { visibility: visible; } .menu-wrap .toggler:checked ~ .menu > div { /* transform: scale(1); */ transition: 0.2s ease-in-out; transform: translateY(0); } .menu-wrap .toggler:checked ~ .menu > div > div { opacity: 1; transition: opacity 0.1s ease 0.1s; } /* Menu overlay */ .menu-wrap .menu { position: fixed; top: 0; left: 0; width: 100%; height: 100%; visibility: hidden; overflow: hidden; display: flex; align-items: center; justify-content: center; } .menu-wrap .menu > div { background: rgba(0, 0, 0, 0.5); width: 200vw; height: 200vw; display: flex; flex: none; align-items: center; justify-content: center; /* transform: scale(0); */ transform: translateY(20%); /* transition: all 0.1s ease; */ transition: 0.2s ease-in-out; /* border-radius: 50%; */ } .menu-wrap .menu > div > div { text-align: center; max-width: 90vw; max-height: 100vh; opacity: 0; transition: opacity 0.4s ease; } .menu-wrap .menu > div > div > ul > li { list-style: none; font-size: 3rem; font-weight: 500; padding: 0.3rem; } .menu-wrap .menu > div > div > ul > li > a { color: #fff; text-decoration: none; text-transform: uppercase; } /* Hamburger centered */ .h-center { position: absolute; right: 48% !important; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" href="css/style.css" /> <link rel="stylesheet" href="css/menu.css" /> <title>Мышонок</title> </head> <body style="background: #000"> <!-- Hamburger --> <div class="menu-wrap"> <input type="checkbox" class="toggler h-center" /> <div class="hamburger h-center"><div></div></div> <div class="menu"> <div> <div> <ul> <li><a href="personal.html" class="active">Personal</a></li> <li><a href="men.html">Men</a></li> <li><a href="women.html">Women</a></li> <li><a href="video.html">Video</a></li> <li><a href="about.html">About</a></li> </ul> </div> </div> </div> </div> <div class="pimg1"></div> <div class="pimg2"></div> </body> </html>

1) I have a page with content. 2) I have a hamburger menu which when clicked brings the off-canvas menu

I need:

To not be able to scroll my page with content when I am in the off-canvas menu. How to achieve that?

PS I can add the code if that is needed. But perhaps it is simple question for someone. Tell me if you need the code

you can set body overflow to hidden using javascript or jquery or add a class

CSS:

.scroll-off{
   overflow: hidden;
}

script:

$(body).addClass('scroll-off');

or

$(body).css('overflow', 'hidden');

for more information, you can read more about jquery in w3schools.com

Example:

 <html> <head> <title>title</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> </head> <body> <button>Click Me</button> <div style="width: 300px; height: 1000px; background-color: red;margin: 0px auto;"></div> </body> <script> var index = 0; $('button').click(function () { if(index == 0){ $('body').css('overflow', 'hidden'); index = 1; }else if(index == 1){ $('body').css('overflow', 'auto'); index = 0; } }); </script> </html>

this is your file whit script code:

 @import url("https://fonts.googleapis.com/css?family=Oswald:200,300,400,500,600,700&display=swap"); * { margin: 0px; padding: 0px; box-sizing: border-box; } body { font-family: "Oswald", sans-serif; height: 100%; overflow: hidden; } img { width: 100%; } /* Utility */ .container { margin: 0 auto; max-width: 1160px; padding: 0 20px; overflow: hidden; } .overlay { top: 0; left: 0; right: 0; bottom: 0; position: absolute; background-color: rgba(0, 0, 0, 0.5); } .hidden { display: none; } .dark { background: #000; } .active { opacity: 0.7; } .parent { position: relative; } /* Home */ .showcase { position: relative; height: 100vh; background-image: url(../img/homebg/bg1.jpg); background-position: center; background-repeat: no-repeat; background-size: cover; animation: slide 18s infinite; transition: 100ms ease-in-out; width: 100%; } .showcase h1 { font-size: 3.5rem; font-weight: 500; color: #fff; padding-top: 40px; text-transform: uppercase; position: absolute; z-index: 1; } @keyframes slide { 0% { background-image: url(../img/homebg/bg1.jpg); } 20% { background-image: url(../img/homebg/bg2.jpg); } 40% { background-image: url(../img/homebg/bg3.jpg); } 60% { background-image: url(../img/homebg/bg4.jpg); } 80% { background-image: url(../img/homebg/bg5.jpg); } 100% { background-image: url(../img/homebg/bg1.jpg); } } /* Personal */ .pimg1 { background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("../img/homebg/bg1.jpg"); min-height: 500px; } .pimg2 { background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("../img/homebg/bg2.jpg"); min-height: 500px; } .ptext1 { color: #fff; display: flex; justify-content: center; align-items: center; padding-top: 300px; font-size: 3rem; font-weight: 500; text-transform: uppercase; } /* Hamburger menu */ .menu-wrap { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .menu-wrap .toggler { top: 55px; right: 280px; cursor: pointer; width: 50px; height: 50px; opacity: 0; position: absolute; z-index: 10; } .menu-wrap .hamburger { top: 55px; right: 270px; width: 70px; height: 60px; padding: 1rem; background: transparent; display: flex; align-items: center; justify-content: center; position: absolute; z-index: 1; } /* Hamburger Icon */ .menu-wrap .hamburger > div { position: relative; flex: none; width: 100%; height: 4px; background: #fff; display: flex; align-items: center; justify-content: center; transition: all 0.4s ease; } .menu-wrap .hamburger > div::before, .menu-wrap .hamburger > div::after { content: ""; position: absolute; z-index: 1; top: -10px; width: 100%; height: 4px; background: inherit; } .menu-wrap .hamburger > div::after { top: 10px; } /* Toggler Animation */ .menu-wrap .toggler:checked + .hamburger > div { transform: rotate(135deg); } .menu-wrap .toggler:checked + .hamburger > div:before, .menu-wrap .toggler:checked + .hamburger > div:after { top: 0; transform: rotate(90deg); } .menu-wrap .toggler:checked:hover + .hamburger > div { transform: rotate(225deg); } /* Show Menu */ .menu-wrap .toggler:checked { overflow: hidden; } .menu-wrap .toggler:checked ~ .menu { visibility: visible; } .menu-wrap .toggler:checked ~ .menu > div { /* transform: scale(1); */ transition: 0.2s ease-in-out; transform: translateY(0); } .menu-wrap .toggler:checked ~ .menu > div > div { opacity: 1; transition: opacity 0.1s ease 0.1s; } /* Menu overlay */ .menu-wrap .menu { position: fixed; top: 0; left: 0; width: 100%; height: 100%; visibility: hidden; overflow: hidden; display: flex; align-items: center; justify-content: center; } .menu-wrap .menu > div { background: rgba(0, 0, 0, 0.5); width: 200vw; height: 200vw; display: flex; flex: none; align-items: center; justify-content: center; /* transform: scale(0); */ transform: translateY(20%); /* transition: all 0.1s ease; */ transition: 0.2s ease-in-out; /* border-radius: 50%; */ } .menu-wrap .menu > div > div { text-align: center; max-width: 90vw; max-height: 100vh; opacity: 0; transition: opacity 0.4s ease; } .menu-wrap .menu > div > div > ul > li { list-style: none; font-size: 3rem; font-weight: 500; padding: 0.3rem; } .menu-wrap .menu > div > div > ul > li > a { color: #fff; text-decoration: none; text-transform: uppercase; } /* Hamburger centered */ .h-center { position: absolute; right: 48% !important; }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" href="css/style.css" /> <link rel="stylesheet" href="css/menu.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <title>Мышонок</title> </head> <body style="background: #000"> <!-- Hamburger --> <div class="menu-wrap"> <input type="checkbox" class="toggler h-center" /> <div class="hamburger h-center"><div></div></div> <div class="menu"> <div> <div> <ul> <li><a href="personal.html" class="active">Personal</a></li> <li><a href="men.html">Men</a></li> <li><a href="women.html">Women</a></li> <li><a href="video.html">Video</a></li> <li><a href="about.html">About</a></li> </ul> </div> </div> </div> </div> <div class="pimg1"></div> <div class="pimg2"></div> <script> var index = 0; $('.toggler').click(function () { if(index == 0){ $('body').css('overflow', 'hidden'); index = 1; }else if(index == 1){ $('body').css('overflow', 'auto'); index = 0; } }); </script> </body> </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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