我有一个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>
我通常做的是添加另一个称为 overlay 的元素,并在切换导航项以产生该效果时切换它。我还会将切换按钮移出导航,以便您可以为其提供绝对位置,因为您需要应用 z 索引以在启用叠加层时保持可见。见下文...希望对您有所帮助!
$(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>
从nav
和main
中删除background-color
(它目前与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>
希望这有帮助!
你可以做这样的事情:
$(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>