当我单击导航按钮时,导航栏保持打开状态。我真的是编码新手,不胜感激!我可以按一下汉堡按钮来打开和关闭。但是,当我单击链接浏览网站时,单击导航栏将保持打开状态。
*{
margin:0;
padding:0;
box-sizing: inherit;
}
.nav {
height: 50px;
width: 100%;
background-color: #4d4d4d;
/* position: relative; */
position: fixed;
z-index: 10;
}
.nav > .nav-header {
display: inline;
}
.nav > .nav-header > .nav-title {
display: inline-block;
font-size: 22px;
color: #fff;
padding: 10px 10px 10px 10px;
}
.nav > .nav-btn {
display: none;
}
.nav > .nav-links {
display: inline;
/* float: right; */
font-size: 18px;
}
.nav > .nav-links > a {
display: inline-block;
padding: 13px 10px 13px 10px;
text-decoration: none;
color: #efefef;
}
.nav > .nav-links > a:hover {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > #nav-check {
display: none;
}
@media (max-width:700px) {
.nav > .nav-btn {
display: inline-block;
position: absolute;
right: 0px;
top: 0px;
}
.nav > .nav-btn > label {
display: inline-block;
width: 50px;
height: 50px;
padding: 13px;
}
.nav > .nav-btn > label:hover,.nav #nav-check:checked ~ .nav-btn > label {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > .nav-btn > label > span {
display: block;
width: 25px;
height: 10px;
border-top: 2px solid #eee;
}
.nav > .nav-links {
position: absolute;
display: block;
width: 100%;
background-color: #333;
height: 0px;
transition: all 0.3s ease-in;
overflow-y: hidden;
top: 50px;
left: 0px;
z-index: 10;
}
.nav > .nav-links > a {
display: block;
width: 100%;
}
.nav > #nav-check:not(:checked) ~ .nav-links {
height: 0px;
}
.nav > #nav-check:checked ~ .nav-links {
height: calc(100vh - 50px);
overflow-y: auto;
}
}
<div class="nav">
<input type="checkbox" id="nav-check">
<div class="nav-header">
<div class="nav-title">
Gallegos Landscaping
</div>
</div>
<div class="nav-btn">
<label for="nav-check">
<span></span>
<span></span>
<span></span>
</label>
</div>
<div class="nav-links">
<a href="#">Home</a>
<a href="#about1">About</a>
<a href="#services1">Services</a>
<a href="#">Contact</a>
</div>
</div>
答案 0 :(得分:3)
这就像您使用复选框来控制可见性一样,这实际上是很聪明的,而且我之前从未见过,但是在此用例中有所限制。
我认为一种更简单,更动态的方法是添加一些简单的JavaScript。您可以在菜单按钮上添加onClick
处理函数,通过添加CSS来显示/隐藏菜单。
例如:
// JS
const menu = document.querySelector('.menu');
const menuToggle = document.querySelector('.menuToggle');
menuToggle.addEventListener('click', (e) => {
e.preventDefault();
if (window.getComputedStyle(menu).display === 'block') {
menu.style.display = 'none';
} else {
menu.style.display = 'block';
}
});
这样,您可以更好地控制打开/关闭菜单。现在,它保持打开状态,因为复选框处于选中状态,但未更新
答案 1 :(得分:0)
我认为仅靠CSS不可能实现您所需要的。 因此,我添加了一个jQuery解决方案。
我在这里所做的是在单击链接后取消选中该复选框。因此,这触发了下拉菜单的关闭。
$('.nav-links > a').on('click', ()=>{
$('#nav-check').prop('checked', false);
});
*{
margin:0;
padding:0;
box-sizing: inherit;
}
.nav {
height: 50px;
width: 100%;
background-color: #4d4d4d;
/* position: relative; */
position: fixed;
z-index: 10;
}
.nav > .nav-header {
display: inline;
}
.nav > .nav-header > .nav-title {
display: inline-block;
font-size: 22px;
color: #fff;
padding: 10px 10px 10px 10px;
}
.nav > .nav-btn {
display: none;
}
.nav > .nav-links {
display: inline;
/* float: right; */
font-size: 18px;
}
.nav > .nav-links > a {
display: inline-block;
padding: 13px 10px 13px 10px;
text-decoration: none;
color: #efefef;
}
.nav > .nav-links > a:hover {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > #nav-check {
display: none;
}
@media (max-width:1000px) {
.nav > .nav-btn {
display: inline-block;
position: absolute;
right: 0px;
top: 0px;
}
.nav > .nav-btn > label {
display: inline-block;
width: 50px;
height: 50px;
padding: 13px;
}
.nav > .nav-btn > label:hover,.nav #nav-check:checked ~ .nav-btn > label {
background-color: rgba(0, 0, 0, 0.3);
}
.nav > .nav-btn > label > span {
display: block;
width: 25px;
height: 10px;
border-top: 2px solid #eee;
}
.nav > .nav-links {
position: absolute;
display: block;
width: 100%;
background-color: #333;
height: 0px;
transition: all 0.3s ease-in;
overflow-y: hidden;
top: 50px;
left: 0px;
z-index: 10;
}
.nav > .nav-links > a {
display: block;
width: 100%;
}
.nav > #nav-check:not(:checked) ~ .nav-links {
height: 0px;
}
.nav > #nav-check:checked ~ .nav-links {
height: calc(100vh - 50px);
overflow-y: auto;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="nav">
<input type="checkbox" id="nav-check">
<div class="nav-header">
<div class="nav-title">
Gallegos Landscaping
</div>
</div>
<div class="nav-btn">
<label for="nav-check">
<span></span>
<span></span>
<span></span>
</label>
</div>
<div class="nav-links">
<a href="#">Home</a>
<a href="#about1">About</a>
<a href="#services1">Services</a>
<a href="#">Contact</a>
</div>
</div>
答案 2 :(得分:0)
这是可以解决您问题的代码。
我已经在您的代码中添加了一个小脚本,该脚本将在单击菜单项时关闭导航栏。
data PromptResult a = Done a | NeedsMoreInput (Prompt a)
purePrompt :: Prompt a -> [Text] -> ([Text], PromptResult a)
purePrompt = {- ... -}