如何在显示侧边栏时禁用背景,单击除侧边栏之外的任何位置都会关闭侧边栏
How to disable the background when sidebar is shown and clicking on the anywhere except sidebar will close the sidebar
我正在为我的网站制作侧边栏。
- 当我的侧边栏出现时(单击showRight),我想禁用背景内容,这样用户就不能在菜单之外做任何事情
- 当用户点击后台时,侧边栏将消失,然后他们将再次访问后台内容。现在,当点击隐藏按钮时,我正在关闭侧边栏
有人能帮我吗?
这是我的代码:
<html>
<head>
<style>
.cbp-spmenu {
background: #fff;
position: fixed;
}
.cbp-spmenu-vertical {
width: 400px;
height: 100%;
top: 0;
z-index: 1000;
}
.cbp-spmenu-right {
right: -400px;
}
.cbp-spmenu, #cbp-spmenu-push {
-webkit-transition: all 0.3s ease;
-moz-transition: all 0.3s ease;
transition: all 0.3s ease;
}
</style>
</head>
<body>
<div id="menu">
<span class="cbp-spmenu cbp-spmenu-vertical cbp-spmenu-right" id="cbp-spmenu-s1">
<h4>Avinash</h4>
</span>
</div>
<div id="content">
<section id="about">
<button id="showRight">Show</button>
<button id="showRight2">Hide</button>
</section>
<script>
var menuRight = document.getElementById( 'cbp-spmenu-s1' ),
showRight = document.getElementById( 'showRight' ),
showRight2 = document.getElementById( 'showRight2' ),
content = document.getElementById( 'avi' ),
menu = document.getElementById( 'menu' ),
body = document.body;
showRight.onclick = function() {
classie.add(this,"active");
menuRight.style.right = '0px';
content.style.opacity = '0.5';
body.style.display = 'block';
};
showRight2.onclick = function() {
if (classie.has(showRight,"active")) {
menuRight.style.right = '-400px';
classie.remove(showRight,"active");
content.style.opacity = '1';
}
};
</script>
</div>
</body>
</html>
在我看来,您可以创建一个覆盖除侧边栏之外的整个站点的div
将这个div放在您的body标签后面,如下所示:
<div class="overlay"></div>
在您的css中:
body{
overflow-y: hidden;
}
.overlay{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 5000px;
z-index: -1;
}
然后使用javascript将该div的z-index
值更改为9999或任何涵盖所有内容的值。
更新:将css中的侧边栏z索引设置为高于此新值的值
可能并不理想,但如果你没有得到更好的答案,请考虑一下。
您应该添加覆盖块。
<div class="overlay"></div>
.overlay {
position: fixed;
width: 100%;
height: 100%;
z-index: 999; /* sidebar must have z-index > 999 */
background: white;
opacity: 0; /* or background set to rgba(255, 255, 255, 0); */
}
相关文章:
- 当侧边栏很长时,滚动/固定侧边栏会被切断
- 创建带有侧边栏的谷歌地图
- 如何在显示侧边栏时禁用背景,单击除侧边栏之外的任何位置都会关闭侧边栏
- 在页脚处停止固定侧边栏-防止重叠
- 如何将javascript添加到Wordpress侧边栏中
- 如何正确地将数据发送到打开的Firefox侧边栏
- 如何制作侧边栏以增加其完整长度
- 固定位置侧边栏不滚动-设置内容的高度
- Firefox插件SDK:在侧边栏和主脚本之间通信对象
- 在上下文菜单项单击上显示侧边栏/弹出窗口的最佳方法是什么
- 如何将数组(Google应用程序脚本)返回到HTML侧边栏
- 切换传单侧边栏 V2
- 粘性侧边栏停止在页脚上方 30px
- 使用什么js函数来切换侧边栏
- 指定的侧边栏广播器无效 -- 有效的侧边栏广播器是什么样子的
- 向下滚动页面时在侧边栏中显示部分标题(也许使用 BS scrollspy?
- 火狐侧边栏获取标签网址
- 滚动时固定了侧边栏的计算
- ExtJS 4.1.它们是创建侧边栏菜单的最佳组件(如左面板中的Worpress)
- 可折叠侧边栏/导航菜单