我有off-screen nav
的模板,当点击toggle
时,它会从左侧滑入。我想要发生的事情是当nav
滑入时,叠加层会使其背后的内容变暗。为此,我将页面内容包装在div
中并尝试添加BG颜色以覆盖内容,但它似乎无法正常工作。
有人可以帮忙吗?代码如下:
$(document).ready(function() {
var toggle = $('.toggle');
var osNav = $('.offScreen-nav');
var pageContainer = $('.page-container');
toggle.click(function() {
osNav.toggleClass('showNav');
pageContainer.toggleClass('navOnScreen')
});
})

* {
margin: 0;
padding: 0;
overflow: hidden;
}
nav {
width: 100%;
height: 70px;
//border: 1px solid;
background-color: #f1f1f1;
display: flex;
flex-direction: row;
justify-content: flex-end;
}
.toggle {
height: 50px;
width: 50px;
border: 1px solid;
background-color: rgba(17,17,17, .9);
opacity: .9;
margin: 20px 15px;
cursor: pointer;
align-self: center;
}
main {
height: 1000px;
background-color: #ddd;
}
.offScreen-nav {
width: 340px;
height: 100vh;
position: fixed;
margin-left: 0;
overflow: hidden;
z-index: 2000;
background-color: rgba(17,17,17, .9);
transition: all .3s ease-in-out;
transform: translate3d(-340px,0,0);
}
.showNav {
transform: translate3d(0,0,0);
}
.page-container {
transform: translate3d(0,0,0);
transition: all .3s ease-in-out;
transition-delay: .1s;
}
.page-container.navOnScreen {
background-color: rgba(0,0,0, .6);
transform: translate3d(20px,0,0);
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="offScreen-nav"></div>
<div class="page-container">
<nav>
<div class="toggle"></div>
</nav>
<main></main>
</div>
&#13;
答案 0 :(得分:1)
从background-color
&amp;移除nav
main
也是{它当前与body
的背景颜色重叠),例如,
在JS中:
// Toggle a class which removes background-color from main & nav
nav.toggleClass('remove-bg')
main.toggleClass('remove-bg')
在CSS中:
/* Resetting background-color to transparent */
.remove-bg {
background: transparent;
}
/* To get the transition well */
main, nav {
transition: all .3s ease-in-out;
transition-delay: .1s;
}
请看下面的代码:
$(document).ready(function() {
var toggle = $('.toggle');
var osNav = $('.offScreen-nav');
var pageContainer = $('.page-container');
var nav = $('nav');
var main = $('main');
toggle.click(function() {
osNav.toggleClass('showNav');
pageContainer.toggleClass('navOnScreen')
nav.toggleClass('remove-bg')
main.toggleClass('remove-bg')
});
})
* {
margin: 0;
padding: 0;
overflow: hidden;
}
nav {
width: 100%;
height: 70px;
//border: 1px solid;
background-color: #f1f1f1;
display: flex;
flex-direction: row;
justify-content: flex-end;
transition: all .3s ease-in-out;
transition-delay: .1s;
}
.toggle {
height: 50px;
width: 50px;
border: 1px solid;
background-color: rgba(17,17,17, .9);
opacity: .9;
margin: 20px 15px;
cursor: pointer;
align-self: center;
}
main {
height: 1000px;
background-color: #ddd;
transition: all .3s ease-in-out;
transition-delay: .1s;
}
.offScreen-nav {
width: 340px;
height: 100vh;
position: fixed;
margin-left: 0;
overflow: hidden;
z-index: 2000;
background-color: rgba(17,17,17, .9);
transition: all .3s ease-in-out;
transform: translate3d(-340px,0,0);
}
.showNav {
transform: translate3d(0,0,0);
}
.page-container {
transform: translate3d(0,0,0);
transition: all .3s ease-in-out;
transition-delay: .1s;
}
.page-container.navOnScreen {
background-color: rgba(0,0,0, .6);
transform: translate3d(20px,0,0);
}
.remove-bg {
background: transparent;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="offScreen-nav"></div>
<div class="page-container">
<nav>
<div class="toggle"></div>
</nav>
<main></main>
</div>
希望这有帮助!
答案 1 :(得分:1)
您可以这样做:
$(document).ready(function() {
var toggle = $('.toggle');
var osNav = $('.offScreen-nav');
var pageContainer = $('.page-container');
var darkLayer = $('main, nav');
toggle.click(function() {
$(this).toggleClass('border');
osNav.toggleClass('showNav');
darkLayer.toggleClass('darkLayer');
pageContainer.toggleClass('navOnScreen');
});
})
* {
margin: 0;
padding: 0;
overflow: hidden;
}
nav {
width: 100%;
height: 70px;
//border: 1px solid;
background-color: #f1f1f1;
display: flex;
flex-direction: row;
justify-content: flex-end;
}
.toggle {
height: 50px;
width: 50px;
border: 1px solid;
background-color: rgba(17,17,17, .9);
opacity: .9;
margin: 20px 15px;
cursor: pointer;
align-self: center;
}
main {
height: 1000px;
background-color: #ddd;
}
.offScreen-nav {
width: 340px;
height: 100vh;
position: fixed;
margin-left: 0;
overflow: hidden;
z-index: 2000;
background-color: rgba(17,17,17, .9);
transition: all .3s ease-in-out;
transform: translate3d(-340px,0,0);
}
.showNav {
transform: translate3d(0,0,0);
}
.page-container {
transform: translate3d(0,0,0);
transition: all .3s ease-in-out;
transition-delay: .1s;
}
.page-container.navOnScreen {
background-color: rgba(0,0,0,.6);
transform: translate3d(20px,0,0);
}
.darkLayer {
background: #333;
opacity: .5;
transition: all .3s linear;
}
.border {
border-color: #fff;
transition: all .3s linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="offScreen-nav"></div>
<div class="page-container">
<nav>
<div class="toggle"></div>
</nav>
<main></main>
</div>
答案 2 :(得分:1)
我通常做的是添加另一个名为overlay的元素,并在切换导航项以产生该效果时切换它。我还会将导航按钮从导航中移出,以便您可以给它一个绝对位置,因为您需要应用z-index以在启用叠加时使其保持可见状态。见下文......希望它有所帮助!
$(document).ready(function() {
var toggle = $('.toggle');
var osNav = $('.offScreen-nav');
var overlay = $('.overlay');
var pageContainer = $('.page-container');
toggle.click(function() {
osNav.toggleClass('showNav');
overlay.toggleClass('showNav');
pageContainer.toggleClass('navOnScreen')
});
})
* {
margin: 0;
padding: 0;
overflow: hidden;
}
nav {
width: 100%;
height: 70px;
//border: 1px solid;
background-color: #f1f1f1;
display: flex;
flex-direction: row;
justify-content: flex-end;
}
.toggle {
height: 50px;
width: 50px;
border: 1px solid;
background-color: rgba(17,17,17, .9);
opacity: .9;
/* margin: 20px 15px; */
cursor: pointer;
align-self: center;
position: absolute;
top: 10px;
right: 15px;
z-index: 2;
}
main {
height: 1000px;
background-color: #ddd;
}
.offScreen-nav {
width: 340px;
height: 100vh;
position: fixed;
margin-left: 0;
overflow: hidden;
z-index: 2000;
background-color: rgba(17,17,17, .9);
transition: all .3s ease-in-out;
transform: translate3d(-340px,0,0);
}
.showNav {
transform: translate3d(0,0,0);
}
.page-container {
transform: translate3d(0,0,0);
transition: all .3s ease-in-out;
transition-delay: .1s;
}
.page-container.navOnScreen {
background-color: rgba(0,0,0, .6);
transform: translate3d(20px,0,0);
}
.overlay.showNav {
width: 100vw;
height: 100vh;
background: blue;
position: fixed;
z-index: 1;
}
.overlay{
transition:0.3s ease;
opacity: 0.5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="offScreen-nav"></div>
<div class="overlay"></div>
<div class="toggle"></div>
<div class="page-container">
<nav>
</nav>
<main></main>
</div>