(Jsfiddle用于上下文:https://jsfiddle.net/hfwtq2c9/12/)
我做了一个导航栏,为了简洁,一些链接在下拉菜单中。当屏幕太小时,它会将所有链接折叠成一个菜单,可以通过一个按钮访问。我想让它进入菜单时,所有链接都从下拉菜单中出来,进入一个列表。我还有一个问题,下拉菜单中的链接在左边有一些随机的空白,我不知道为什么会这样。这可以做到吗?谢谢
我的代码:
const primaryNav = document.querySelector(".primary-navigation");
const navToggle = document.querySelector(".mobile-nav-toggle");
navToggle.addEventListener("click", () => {
const visibility = primaryNav.getAttribute("data-visible");
if (visibility === "false") {
primaryNav.setAttribute("data-visible", true);
navToggle.setAttribute("aria-expanded", true);
} else if (visibility === "true") {
primaryNav.setAttribute("data-visible", false);
navToggle.setAttribute("aria-expanded", false);
}
});
@import url("https://fonts.googleapis.com/css2?family=Poppins&display=swap");
* {
font-family: "Poppins", sans-serif;
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
background: radial-gradient(circle,
rgba(0, 102, 215, 1) 0%,
rgba(15, 0, 157, 1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#0066d7", endColorstr="#0f009d", GradientType=1);
}
a {
text-decoration: none;
}
.sr-only {
display: none;
}
/* primary nav */
.flex {
display: flex;
gap: 2rem;
}
.primary-header {
align-items: center;
justify-content: space-between;
background: rgba(0, 0, 0, 0.4);
}
.primary-navigation {
list-style: none;
padding: 2rem;
}
.primary-navigation a {
color: #ffffff;
text-decoration: none;
font-size: 1.5rem;
transition: all 300ms ease-in-out;
list-style: none;
}
.primary-navigation a:hover {
color: #929292;
}
.mobile-nav-toggle {
display: none;
}
@media (max-width: 50rem) {
.primary-navigation {
gap: 2rem;
position: fixed;
z-index: 1000;
inset: 0 0 0 0;
padding: 10rem 3rem;
flex-direction: column;
transform: translateX(100%);
transition: transform 300ms ease-in-out;
margin: 0;
backdrop-filter: blur(1rem);
background: rgba(0, 0, 0, 0.4);
}
.primary-navigation a {
font-size: 1.75rem;
}
.primary-navigation[data-visible="true"] {
transform: translateX(0%);
}
.primary-header .mobile-nav-toggle {
display: block;
position: absolute;
z-index: 9999;
background-image: url("./assets/hamburger.svg");
background-color: hotpink;
background-size: cover;
background-repeat: no-repeat;
width: 2.5rem;
border: 0;
aspect-ratio: 1;
right: 2rem;
}
.primary-header .mobile-nav-toggle[aria-expanded="true"] {
background-image: url(./assets/close.svg);
position: fixed;
}
.primary-header {
padding: 4rem;
}
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgba(0, 0, 0, 0.5);
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
list-style: none;
}
.dropdown-content a {
color: #ffffff;
padding: 1rem;
display: flex;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: flex;
flex-direction: column;
}
<header class="primary-header flex">
<button class="mobile-nav-toggle" aria-controls="primary-navigation" aria-expanded="false">
<span class="sr-only">Menu</span>
</button>
<nav>
<ul data-visible="false" class="primary-navigation flex">
<li><a href="index.html">Home</a></li>
<li class="dropdown">
<a href="about.html" class="dropbtn">About Me</a>
<ul class="dropdown-content">
<li><a href="projects.html">My previous Projects</a></li>
<li><a href="quals.html">My Qualifications</a></li>
<li><a href="cv.html">My CV/Resume</a></li>
<li><a href="services.html">My Services</a></li>
</ul>
</li>
<li class="dropdown">
<a href="contact.html" class="dropbtn">Contact Me</a>
<ul class="dropdown-content">
<li><a href="social.html">Social Media</a></li>
</ul>
</li>
</ul>
</nav>
</header>
2条答案
按热度按时间6kkfgxo01#
为了消除空白,可以将
padding-left: 0
添加到.dropdown-content
类中。c2e8gylq2#
如果我对您的问题理解正确的话,您可能需要对
.dropdown-content
的样式进行一些调整。至于你的下拉链接左边的随机空格是由填充引起的。
希望这有帮助:)