[英]Block User scroll when full screen menu is open
我正在為網站制作全屏菜單,但我需要在菜單打開時禁用用戶滾動。 我在互聯網上找不到合適的解決方案,所以如果有人可以幫助我,那就太好了。 如果菜單打開,我不確定如何觸發正文不滾動。 我對js不是很熟悉。
這是我目前的代碼:
<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>
這就是 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;
}
謝謝你的幫助!
我在菜單打開時使用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.