[英]How do I get the dropdown navigation bar to work with html and css?
I've been trying to make a dropdown navigation bar (having the links centered) for the mobile view, with only html and css (separate javascript file isn't needed, as the project isn't that dense).我一直在尝试为移动视图制作一个下拉导航栏(链接居中),只有 html 和 css(不需要单独的 javascript 文件,因为项目不是那么密集)。 I think I missed something earlier on, but I can't find the problem anymore.
我想我之前错过了一些东西,但我再也找不到问题了。 Can someone make it work?
有人可以让它工作吗? Doesn't matter what method you use.
不管你用什么方法。 If more parts of the code is needed, please inform me.
如果需要更多部分的代码,请通知我。
html navbar section: html 导航栏部分:
<!-- Navigation bar section -->
<header class="navbar">
<div class='nav_container'>
<ion-icon class="mobile-menu-icon" id="mobile-cta" name="menu-outline"></ion-icon>
<nav>
<ion-icon class="mobile-close-icon" id="mobile-exit" name="close-outline"></ion-icon>
<ul class="primary_nav">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</div>
</header>
script inside html: html里面的脚本:
<script>
const mobileBtn = document.getElementById('mobile-cta')
nav = document.querySelector('nav')
mobileBtnExit = document.getElementById('mobile-exit');
mobileBtn.addEventListener('click', () => {
nav.classList.toggle('menu-btn');
})
mobileBtnExit.addEventListener('click', () => {
nav.classList.toggle('menu-btn');
})
</script>
scss: scss:
.navbar {
background: black;
.navbar_container {
display: flex;
place-content: space-between;
}
nav{
display: none;
position: fixed;
width: 100%;
height: 58vh;
padding: 1em;
top: 0;
z-index: 999;
background: var(--primary-blue);
ul {
margin-top: 2.1em;
}
li {
a {
display: block;
padding: .9em;
font-size: 2em;
font-weight: 500;
text-align: center;
opacity: 0.85;
&:hover {
opacity: 1;
transition: all 0.4 ease;
}
}
}
}
}
.mobile-close-icon {
position: absolute;
right: 0px;
margin-right: 1em;
cursor: pointer;
font-size: 3em;
color: white;
opacity: 0.85;
&:hover {
opacity: 1;
}
}
.mobile-menu-icon {
cursor: pointer;
color: white;
position: absolute;
right: 0px;
margin-right: 1em;
top: 15px;
width: 40px;
height: 40px;
}
It looks like you haven't defined your const
declarations correctly.看起来您没有正确定义
const
声明。 Try something like below:尝试如下:
<!-- Option 1: Explictly defined `const` definitions -->
<script>
const mobileBtn = document.getElementById('mobile-cta');
const nav = document.querySelector('nav');
const mobileBtnExit = document.getElementById('mobile-exit');
mobileBtn.addEventListener('click', () => {
nav.classList.toggle('menu-btn');
})
mobileBtnExit.addEventListener('click', () => {
nav.classList.toggle('menu-btn');
})
</script>
<!-- Option 2: "Inlined" const definitions -->
<script>
const mobileBtn = document.getElementById('mobile-cta'),
nav = document.querySelector('nav'),
mobileBtnExit = document.getElementById('mobile-exit');
mobileBtn.addEventListener('click', () => {
nav.classList.toggle('menu-btn');
})
mobileBtnExit.addEventListener('click', () => {
nav.classList.toggle('menu-btn');
})
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.