jquery - 导航栏隐藏和显示在小屏幕上的切换会弄乱大屏幕
问题描述
我一直在尝试使用 Flexbox 使我的导航栏可折叠。当屏幕变小时,我让导航栏正确切换。但是当您在较小的屏幕上隐藏导航项目然后将屏幕放大时,导航项目将不会显示。如果我在屏幕较小时将其打开,然后将屏幕变大,导航项目的 flex 方向将设置为当屏幕较大时我不想要的列。
这是我的代码:
$(".toggle-btn").click(function() {
$(".nav-toggle").slideToggle("slow");
});
/* header */
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 2em;
background-color: #FFB600;
box-shadow: 0 1px 20px #48493e;
z-index: 1;
}
header ul {
display: flex;
}
/*slide menu*/
.nav-btn {
display: none;
position: absolute;
top: 0;
right: 0;
margin: 3.5em 4em 0 0;
}
.toggle-btn {
position: relative;
padding: 0;
width: 4em;
height: 2em;
cursor: pointer;
outline: none;
font-size: 1.2em;
font-family: 'Dosis', sans-serif;
font-weight: bold;
box-shadow: 5px 5px 5px 1px #000;
text-transform: uppercase;
border-radius: 4px;
border: solid 2px #000;
background-color: transparent;
}
.toglge-btn:active {
box-shadow: 3px 3px 5px 1px #000;
top: 2px;
}
@media all and (max-width: 820px) {
header {
flex-direction: column;
}
.nav-brand {
align-self: flex-start;
}
.nav-btn {
display: block;
}
header ul {
flex-direction: column;
text-align: center;
display: none;
}
.nav-items {
line-height: 2;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<h2 class="nav-brand"><a href="/"> Tequila Grill</a></h2>
<div class="nav-btn">
<button class="toggle-btn">Menu</button>
</div>
<ul class="nav-toggle">
<li><a class="nav-items" href="/">Home</a></li>
<li><a class="nav-items" href="/menu">Menu</a></li>
<li><a class="nav-items" href="/hours">Hours</a></li>
<li><a class="nav-items" href="/location">Location</a></li>
<li><a class="nav-items" href="/catering">Catering</a></li>
</ul>
</header>
解决方案
function resiz() {
$(".toggle-btn").click(function() {
$(".nav-toggle").slideToggle("slow");
});
}
resiz();
$(window).on('resize', function() {
resiz();
});
它会起作用
推荐阅读
- python - 为什么这个双循环在 python 中比 matlab 慢得多?
- python - ValueError:检查目标时出错:预期dense_4的形状为(9,),但数组的形状为(8,)
- loops - OpenMP:不同的内存位置会加速循环吗?
- java - Java 应用程序的 Google Cloud App Engine 部署失败
- javascript - 我的刽子手游戏在显示您已获胜的警报之前不显示完整的单词
- javascript - 导航离子 4 香草 Javascript
- python - Python3:按值对 3 级嵌套字典进行排序
- c++ - 无法使用 ofstream 写入 Windows 7 中的临时文件夹
- node.js - 如何让不同的用户连接到同一个socket?
- django - Django:访问非唯一字段