javascript - 显示整个页面导航阴影
问题描述
解决方案
Use your <button class="nav">
elements to switch between states. A state can be defined in CSS, like how does it look by default and how does it look when activated. In your case you'll want to change the state of the <div class="site-nav">
element to hide by default, and show when clicking the button.
Remove the onclick
attributes on the buttons as you don't want to navigate. Instead select them in JavaScript and listen for the click. Give them both the same event handler function that will toggle a class on the site-nav
element.
In your CSS add opacity: 0
and visibility: hidden
to the .site-nav
styles. This will make the element transparent and hide it completely. These properties also allow you to use a transition from visible to invisible and vice versa.
const navButtons = document.querySelectorAll('button.nav');
const siteNav = document.querySelector('.site-nav');
function onClick(event) {
siteNav.classList.toggle('active');
}
navButtons.forEach(button => button.addEventListener('click', onClick));
.site-header {
height: 80px;
background-color: #FFFFFF;
display: inline-flex;
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 1;
box-shadow: 0px 0.5px 10px #000000;
}
.site-header-fill {
height: 80px;
}
.site-logo-container {
height: 60px;
margin-left: 20px;
margin-right: auto;
margin-top: 10px;
margin-bottom: 10px;
display: block;
float: left;
}
.site-logo {
height: 60px;
width: auto;
float: left;
}
.site-nav-action-container {
height: 50px;
width: 50px;
max-width: 50px;
margin-left: 10px;
margin-right: 10px;
margin-top: 15px;
margin-bottom: 15px;
display: block;
float: right;
text-align: right;
}
.site-nav {
height: 100%;
left: 0px;
position: fixed;
top: 0px;
width: 100%;
background: #3399ff;
z-index: 2;
opacity: 0;
visibility: 0;
transition: opacity 150ms ease-in-out, visibility 150ms ease-in-out;
}
.site-nav.active {
opacity: 1;
visibility: visible;
}
.site-nav-content {
width: 20%;
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
@media only screen and (max-width: 500px) {
.site-nav-content {
width: auto;
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
}
.site-nav-pages {
text-align: center;
}
<div class="site-header ">
<div class="site-logo-container">
<img class="site-logo" src="https://via.placeholder.com/1000x300" alt="Logo">
</div>
<div class="site-nav-action-container">
<button class="nav">
<p class="site-log-in-text w700">☰</p>
</button>
</div>
</div>
<div class="site-nav">
<div class="site-nav-action-container">
<button class="nav">
<p class="site-log-in-text w700">X</p>
</button>
</div>
<div class="site-nav-content">
<div class="site-nav-pages">
<p>Page 1</p>
<p>Page 2</p>
<p>Page 3</p>
<p>Page 4</p>
<p>Page 5</p>
</div>
</div>
</div>
推荐阅读
- jquery - WordPress 忽略 Cors 标头
- c# - WindowsDefaultLocation (CW_USEDEFAULT) 在哪里?
- python - 打印错误的元组的简单列表
- python - 运行参数扫描时如何防止内存错误?
- python - 如何修复递归函数
- c# - 缩放一组多边形以监控 WPF 中的分辨率
- logging - F# 的新手 - 通过类绑定 (>>=!) 中缀运算符和引号进行不显眼的日志记录?
- c# - 如何计算在mongodb和c#中有db的时间和速度的积分
- elasticsearch - ElasticSearch 如何处理包含 230m 条目的索引?
- asp.net-mvc - 制作没有视图或控制器的 asp.net razor 模板