我对Jquery和CSS有一点问题,也许标题听起来重复,但我真的搜索过,我找不到解决方案,或者我错过了一些东西。
就是这种情况。我正在建立一个带汉堡菜单的网站,但我希望它显示为灯箱。我发现的第一个问题是灯箱没有固定在移动设备中,所以相反我使用了一个效果很好的叠加层,但在移动设备上,滚动感觉很奇怪。我发现它发生在属性“overflow:auto;”在背景div中,但如果我删除它,叠加层将沿着页面滚动,这正是我不想要的。
所以,我需要背景的属性为“overflow:visible;”当页面加载以使滚动平滑时,并且仅当叠加出现时它将变为“自动”以固定它。我试图在覆盖的jquery脚本中执行此操作,但是当覆盖它关闭时,它保留的新属性和滚动变得很奇怪。
以下是代码:
<div class='navbar-toggle' title='Menu'>
<div class='bar1'></div>
<div class='bar2'></div>
<div class='bar3'></div>
</div>
<nav class="nav-hide overlay">
<ul class="overlay-content">
<li><a href="#">Inicio</a></li>
<li><a href="#">Servicios</a></li>
<li><a href="#">Citas</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Blog</a></li>
</ul>
</nav>
<div class="background-content">
<div id="col">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br><br>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?<br><br>
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure.
To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</div>
</div>
<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
CSS:
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
nav,
.navbar-toggle {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
nav {
position: absolute;
z-index: 10;
background-color: #E2B7C0;
width: 100%;
height: 100%;
text-align: center;
display: table;
color: white;
}
.nav-hide { display: none; }
nav ul {
display: table-cell;
vertical-align: middle;
padding-left: 0 !important;
}
nav ul li { list-style: none; margin-bottom: 15px;}
nav ul li a {
font-size: 25px;
color: inherit;
font-weight: normal;
text-decoration: none;
}
.navbar-toggle {
position:fixed;
top: 30px;
left: 30px;
width: 45px;
height: 45px;
z-index: 20;
cursor: pointer;
}
.bar1,
.bar2,
.bar3 {
width: 100%;
height: 3px;
margin-bottom: 10px;
background-color: #1c1f72;
transition: all 0.3s ease-in-out;
}
.navbar-on .bar1,
.navbar-on .bar2,
.navbar-on .bar3 { background-color: white; }
.navbar-on .bar1 {
transform-origin: 10% 40%;
transform: rotate(45deg);
}
.navbar-on .bar3 {
transform-origin: 10% 40%;
transform: rotate(-45deg);
}
.navbar-on .bar2 { background-color: transparent; }
#col{
margin: 80px 30px;
max-width: 600px;
}
.overlay{
position: fixed;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
}
.overlay .overlay-content {
height: 100%;
overflow: scroll;
}
.background-content{
height: 100%;
}
脚本:
$(function() {
$('.navbar-toggle, nav').click(function(){
$('.navbar-toggle').toggleClass('navbar-on');
$('nav').fadeToggle();
$('nav').removeClass('nav-hide');
$('.overlay').css('display','');
$('.background-content').css('overflow','auto'); //This is the line that I add
});
});
https://jsfiddle.net/Trebohdz/t2nz3aoy/5/
覆盖的代码是我在这里找到的解决方案,我尝试使用.addClass和.removeClass事件,甚至.on和.off,但它不起作用或者我不知道如何集成它代码。
我希望你能帮助我。
答案 0 :(得分:1)
你可以创建一个css类和toggle
: -
$(function() {
$('.navbar-toggle, nav').click(function() {
$('.navbar-toggle').toggleClass('navbar-on');
$('nav').fadeToggle();
$('nav').removeClass('nav-hide');
$('.overlay').css('display', '');
$('.background-content').toggleClass('overflow-auto');
});
});
.overflow-auto {
overflow: auto;
}