画布菜单滑入时如何使主要内容变暗

时间:2019-04-21 07:46:36

标签: javascript css off-canvas-menu

我有一个离开画布的菜单,它从左滑动以覆盖页面。我页面的基础是div。我调用javascript函数来打开和关闭画布菜单。我的主要内容仍然保持在滑动菜单下。在寻找一种使主要内容变暗并阻止对其单击的解决方案时,我遇到了问题。

我尝试使用属性 background:rgba(0,0,0,0.5)!important; 在画布菜单和maincontent之间具有z索引的附加div,但没有任何结果。在这里也到处搜索,但没有找到我想要的东西。而且我不想使用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()">&times;</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滚动,因为它还会拉出画布外的菜单来跟随它,从而使被截断的侧边菜单区域显示出来。

1 个答案:

答案 0 :(得分:0)

首先将主要内容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()">&times;</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()">&times;</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">&times;</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>