javascript - 锁定正文滚动,但正文中的某些 div 可以滚动
问题描述
在移动设备中,当用户触摸(.menu-btn)时,会显示(.menu)并且(body)被锁定滚动。
但问题是(.menu)也被看了。
即使(正文)看起来滚动,我怎样才能使(.menu)可以滚动?
https://jsfiddle.net/n17qw8sb/
我知道有body-scroll-lock。:https ://github.com/willmcpo/body-scroll-lock
不幸的是,我不允许为此使用 webpack :(
<body>
<header>
<h2> Top area </h2>
<div class="menu-btn">
</div>
<div class="menu">
<h2> Hamburger menu area </h2>
</div>
</header>
<section class="contents">
<h2> Contents area </h2>
</section>
</body>
$('.menu-btn').on('click', function() {
$('.menu').toggleClass('active');
$('body').toggleClass('lock-scroll');
$('html').toggleClass('lock-scroll');
})
body {
width: 100%;
background-color: lavender;
}
body.lock-scroll {
overflow: hidden;
}
html.lock-scroll {
overflow: hidden;
}
header {
width: 100%;
height: 3em;
position: fixed;
top: 0;
background-color: beige;
z-index: 100;
}
.menu-btn {
width: 1em;
height: 1em;
background-color: lightseagreen;
position: fixed;
z-index: 300;
top: 1em;
right: 1em;
}
.menu {
width: 100%;
height: 130vh;
position: fixed;
top: 0;
z-index: 200;
padding-top: 40%;
background-color: rgba(255,255,255, .5);
transform: translateX(100%);
transition: all .5s ease;
}
.menu.active {
transform: translateX(0%);
}
.contents {
width: 100%;
padding-top: 50%;
height: 150vh;
background-color: lightblue;
}
h2 {
text-align: center;
}
解决方案
添加一个.menu-content
元素并将元素的内容.menu
放在其中。
并将此样式添加到样式的末尾
.menu {
height: 100%;
overflow: scroll;
}
.menu-content {
height: 130vh;
}
https://jsfiddle.net/dm8zrabx/
$('.menu-btn').on('click', function() {
$('.menu').toggleClass('active');
$('body').toggleClass('lock-scroll');
$('html').toggleClass('lock-scroll');
})
body {
width: 100%;
background-color: lavender;
}
body.lock-scroll {
overflow: hidden;
}
html.lock-scroll {
overflow: hidden;
}
header {
width: 100%;
height: 3em;
position: fixed;
top: 0;
background-color: beige;
z-index: 100;
}
.menu-btn {
width: 1em;
height: 1em;
background-color: lightseagreen;
position: fixed;
z-index: 300;
top: 1em;
right: 1em;
}
.menu {
width: 100%;
height: 130vh;
position: fixed;
top: 0;
z-index: 200;
padding-top: 40%;
background-color: rgba(255,255,255, .5);
transform: translateX(100%);
transition: all .5s ease;
}
.menu.active {
transform: translateX(0%);
}
.contents {
width: 100%;
padding-top: 50%;
height: 150vh;
background-color: lightblue;
}
h2 {
text-align: center;
}
/* newly added style */
.menu {
height: 100%;
overflow: scroll;
}
.menu-content {
height: 130vh;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<header>
<h2> Top area </h2>
<div class="menu-btn">
</div>
<div class="menu">
<div class="menu-content">
<h2> Hamburger menu area </h2>
</div>
</div>
</header>
<section class="contents">
<h2> Contents area </h2>
</section>
</body>
注意:以整页视图查看结果
推荐阅读
- javascript - 单击 eventListner 正在调用我的函数,但前提是我将其传递给匿名函数
- python - 数据框使用第一列作为标题行
- swift - 如何正确编码 url 字符串以避免问号 (?) 在 swift 中成为 %3F?
- google-apps-script - 使用 Google Apps 脚本自动将标签移动/应用到 Gmail
- ios - UITextView 在顶部留下空间
- node.js - 合并节点中的两个对象
- unit-testing - 如何使用 Jest 在 Vuejs 应用程序上监视 window.location.assign?
- ballerina - 如何在 Ballerina 中创建列表列表?
- ios - ARAnchor 在 2D 空间中的位置
- asp.net-web-api2 - 将启动更改为 OWIN 后获取 404 以获取 api 控制器结果