如何在关闭-canvas 菜单滑入时使主要内容变暗
How to dim main content when off-canvas menu slides in
我有一个关闭-canvas 菜单从左侧滑动以覆盖页面。我页面的构建块是 divs。我调用 javascript 函数来打开和关闭 off-canvas 菜单。我的主要内容保持在滑动菜单下。我在寻找使主要内容变暗并阻止对它的任何点击的解决方案时遇到问题。
我尝试使用 属性 背景:rgba(0, 0, 0, 0.5) !important; 额外 div off-canvas 菜单和主要内容之间的 z-index,但没有任何结果。用谷歌搜索,也在这里,但没有找到我要找的东西。而且我不想使用 Bootstrap 或其他库,以使项目尽可能轻便。这是我的代码:
<style>
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px,0,0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
display: none;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, 0) !important;
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
</style>
<script>
function openNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-1px,0,0)";
document.getElementById("cover").style.display = "inline";
document.getElementById("cover").style.background = "rgba(0, 0, 0, 0.5) !important";
}
function closeNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-220px,0,0)";
document.getElementById("cover").style.display = "none";
document.getElementById("cover").style.background = "rgba(0, 0, 0, 0) !important";
}
</script>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" id="closebtn" class="closebtn" onclick="closeNav()">×</a>
//menue items are hereunder
</div>
<div id="cover" class="cover">
<div id ="maincontent" class="maincontent">
//some elements here
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu" onclick="openNav()">
//all the rest of the page is here
</div>
</div>
现在菜单会根据需要滑入和滑出,但我在为以下问题找到简洁且 运行 的解决方案时遇到了问题:
1) 使菜单下的主要内容区域变暗;
2)拦截对主要内容区域的任何点击,要么忽略它,要么点击关闭菜单;
3) 阻止主要内容的 y 滚动,因为它还会拉出关闭的 canvas 菜单跟随它,从而给出一个 截断的 侧面菜单区域的外观。
首先将您的主要内容 div 移到封面 div 之外。目前您的主要内容只有在封面可见时才会可见:
function openNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-1px,0,0)";
document.getElementById("cover").style.display = "inline";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0.5) !important";
}
function closeNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-220px,0,0)";
document.getElementById("cover").style.display = "none";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0) !important";
}
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px, 0, 0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
background: #fff;
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, .5) !important;
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" id="closebtn" class="closebtn" onclick="closeNav()">×</a> menu items are hereunder
</div>
<div id="cover" class="cover">
</div>
<div id="maincontent" class="maincontent">
some elements here<br>
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu" onclick="openNav()"> all the rest of the page is here
</div>
您要点击封面时关闭导航吗?然后只需将您的onclick="closeNav()"
添加到封面即可:
function openNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-1px,0,0)";
document.getElementById("cover").style.display = "inline";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0.5) !important";
}
function closeNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-220px,0,0)";
document.getElementById("cover").style.display = "none";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0) !important";
}
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px, 0, 0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
background: #fff;
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, .5) !important;
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" id="closebtn" class="closebtn" onclick="closeNav()">×</a> menu items are hereunder
</div>
<div id="cover" class="cover" onclick="closeNav()">
</div>
<div id="maincontent" class="maincontent">
some elements here<br>
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu" onclick="openNav()"> all the rest of the page is here
</div>
最后,我建议清理您的代码并使用事件侦听器 而不是 html 中的 onclick 属性。像这样:
var cover = document.getElementById("cover");
var sideNav = document.getElementById("mySidenav");
var closeNavBtn = document.getElementById("closebtn");
var openNavBtn = document.getElementById("open_nav");
function openNav() {
sideNav.style.webkitTransform = "translate3d(-1px,0,0)";
cover.style.display = "inline";
}
function closeNav() {
sideNav.style.webkitTransform = "translate3d(-220px,0,0)";
cover.style.display = "none";
}
closeNavBtn.addEventListener("click", closeNav);
cover.addEventListener("click", closeNav);
openNavBtn.addEventListener("click", openNav);
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px, 0, 0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
background: #fff;
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, .5);
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
<div id="mySidenav" class="sidenav">
<span id="closebtn" class="closebtn">×</span> menu items are hereunder
</div>
<div id="cover" class="cover">
</div>
<div id="maincontent" class="maincontent">
some elements here<br>
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu"> all the rest of the page is here
</div>
我有一个关闭-canvas 菜单从左侧滑动以覆盖页面。我页面的构建块是 divs。我调用 javascript 函数来打开和关闭 off-canvas 菜单。我的主要内容保持在滑动菜单下。我在寻找使主要内容变暗并阻止对它的任何点击的解决方案时遇到问题。
我尝试使用 属性 背景:rgba(0, 0, 0, 0.5) !important; 额外 div off-canvas 菜单和主要内容之间的 z-index,但没有任何结果。用谷歌搜索,也在这里,但没有找到我要找的东西。而且我不想使用 Bootstrap 或其他库,以使项目尽可能轻便。这是我的代码:
<style>
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px,0,0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7,0,0.3,1);
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
display: none;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, 0) !important;
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
</style>
<script>
function openNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-1px,0,0)";
document.getElementById("cover").style.display = "inline";
document.getElementById("cover").style.background = "rgba(0, 0, 0, 0.5) !important";
}
function closeNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-220px,0,0)";
document.getElementById("cover").style.display = "none";
document.getElementById("cover").style.background = "rgba(0, 0, 0, 0) !important";
}
</script>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" id="closebtn" class="closebtn" onclick="closeNav()">×</a>
//menue items are hereunder
</div>
<div id="cover" class="cover">
<div id ="maincontent" class="maincontent">
//some elements here
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu" onclick="openNav()">
//all the rest of the page is here
</div>
</div>
现在菜单会根据需要滑入和滑出,但我在为以下问题找到简洁且 运行 的解决方案时遇到了问题: 1) 使菜单下的主要内容区域变暗; 2)拦截对主要内容区域的任何点击,要么忽略它,要么点击关闭菜单; 3) 阻止主要内容的 y 滚动,因为它还会拉出关闭的 canvas 菜单跟随它,从而给出一个 截断的 侧面菜单区域的外观。
首先将您的主要内容 div 移到封面 div 之外。目前您的主要内容只有在封面可见时才会可见:
function openNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-1px,0,0)";
document.getElementById("cover").style.display = "inline";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0.5) !important";
}
function closeNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-220px,0,0)";
document.getElementById("cover").style.display = "none";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0) !important";
}
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px, 0, 0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
background: #fff;
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, .5) !important;
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" id="closebtn" class="closebtn" onclick="closeNav()">×</a> menu items are hereunder
</div>
<div id="cover" class="cover">
</div>
<div id="maincontent" class="maincontent">
some elements here<br>
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu" onclick="openNav()"> all the rest of the page is here
</div>
您要点击封面时关闭导航吗?然后只需将您的onclick="closeNav()"
添加到封面即可:
function openNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-1px,0,0)";
document.getElementById("cover").style.display = "inline";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0.5) !important";
}
function closeNav() {
document.getElementById("mySidenav").style.webkitTransform = "translate3d(-220px,0,0)";
document.getElementById("cover").style.display = "none";
// document.getElementById("cover").style.background = "rgba(0, 0, 0, 0) !important";
}
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px, 0, 0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
background: #fff;
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, .5) !important;
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" id="closebtn" class="closebtn" onclick="closeNav()">×</a> menu items are hereunder
</div>
<div id="cover" class="cover" onclick="closeNav()">
</div>
<div id="maincontent" class="maincontent">
some elements here<br>
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu" onclick="openNav()"> all the rest of the page is here
</div>
最后,我建议清理您的代码并使用事件侦听器 而不是 html 中的 onclick 属性。像这样:
var cover = document.getElementById("cover");
var sideNav = document.getElementById("mySidenav");
var closeNavBtn = document.getElementById("closebtn");
var openNavBtn = document.getElementById("open_nav");
function openNav() {
sideNav.style.webkitTransform = "translate3d(-1px,0,0)";
cover.style.display = "inline";
}
function closeNav() {
sideNav.style.webkitTransform = "translate3d(-220px,0,0)";
cover.style.display = "none";
}
closeNavBtn.addEventListener("click", closeNav);
cover.addEventListener("click", closeNav);
openNavBtn.addEventListener("click", openNav);
.sidenav {
height: 100%;
width: 220px;
position: absolute;
z-index: 999;
top: 0;
left: 0;
overflow-x: hidden;
-webkit-transform: translate3d(-220px, 0, 0);
-webkit-transition: -webkit-transform 0.4s;
-webkit-transition-timing-function: cubic-bezier(0.7, 0, 0.3, 1);
background: #fff;
}
.cover {
position: absolute;
z-index: 900;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 900;
background: rgba(0, 0, 0, .5);
display: none;
}
.maincontent {
position: relative;
z-index: 20;
margin: auto;
}
<div id="mySidenav" class="sidenav">
<span id="closebtn" class="closebtn">×</span> menu items are hereunder
</div>
<div id="cover" class="cover">
</div>
<div id="maincontent" class="maincontent">
some elements here<br>
<img id="open_nav" class="fixed-ratio-resize_menu" src="menu.png" alt="menu"> all the rest of the page is here
</div>