我有一个菜单,想从浏览器的右侧隐藏它。单击按钮时,菜单将显示并从右向左移动。
一切正常,然而,当菜单被隐藏时,浏览器底部会出现一个水平条!
我试图添加overflow: hidden,但它不起作用。
$(function(){
$('button').click(function(){
$('.head').toggleClass('show');
})
}).head {
width: 100%;
height: 60px;
position: relative;
border: 1px solid red;
}
button {
float: right;
background: black;
color: white;
}
.menu {
width: 300px;
height: 300px;
background: green;
position: absolute;
right: -290px;
top: 60px;
-webkit-transition: all 0.5s ease 0s;
-moz-transition: all 0.5s ease 0s;
-o-transition: all 0.5s ease 0s;
transition: all 0.5s ease 0s;
}
.head.show .menu {
right: 0;
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="head">
<button>Show Menu</button>
<div class="menu"></div>
</div>
发布于 2021-05-04 01:00:28
您需要将body overflow设置为隐藏,它将隐藏滚动条。
body {
overflow:hidden;
}我还将绝对(.menu)中的div移出了容器(.head)。我认为这是一种更好的方式
$(function(){
$('button').click(function(){
$('body').css('overflow', 'hidden');
$('.menu').toggleClass('show');
if (!$('.menu').hasClass('show')) {
$('body').attr('style', '');
}
})
}).container {
position:absolute;
width: 90%;
}
.head {
width: 100%;
height: 60px;
border: 1px solid red;
}
button {
float: right;
background: black;
color: white;
}
.menu {
width: 300px;
height: 300px;
background: green;
position: fixed;
right: -300px;
top: 70px;
-webkit-transition: all 0.5s ease 0s;
-moz-transition: all 0.5s ease 0s;
-o-transition: all 0.5s ease 0s;
transition: all 0.5s ease 0s;
}
.menu.show {
right: 6px;
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="head">
<button>Show Menu</button>
</div>
<div class="menu"></div>
https://stackoverflow.com/questions/67372726
复制相似问题