简体   繁体   English

如何停止页面在画布菜单中滚动?

[英]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. 1) 我有一个包含内容的页面。 2) I have a hamburger menu which when clicked brings the off-canvas menu 2) 我有一个汉堡菜单,点击后会出现画布菜单

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. PS 如果需要,我可以添加代码。 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您可以使用 javascript 或 jquery 将正文溢出设置为隐藏或添加一个类

CSS: 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有关更多信息,您可以在w3schools.com 中阅读有关 jquery 的更多信息

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>

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

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