gpt4 book ai didi

javascript - 响应式中心导航栏

转载 作者:行者123 更新时间:2023-12-04 09:27:11 25 4
gpt4 key购买 nike

我的目标是制作一个以网页为中心的导航栏,并且像这里一样在堆栈中进行响应。
到目前为止,我通过一些研究和教程设法制作了一个响应式导航栏,但它会扩展到 Angular 落的尽头。
我试图像处理内容一样将它包装在一个容器中,但它将整个导航栏限制在中间。
然后尝试向左右添加边距,但是当我进入较小的屏幕时,它变得很难看。
所以我希望你的意见如何解决它,或者是否有其他一些首选的方式来做导航栏。

const toggleButton = document.getElementsByClassName('toggle-button')[0]
const navbarLinks = document.getElementsByClassName('navbar-links')[0]

toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
})
* {
box-sizing: border-box;
}

body {
margin: 0;
padding: 0;
}

.navbar {
display: flex;
position: relative;
justify-content: space-between;
align-items: center;
background-color: #333;
color: white;
}

.brand-title {
font-size: 1.5rem;
margin: .5rem;
}

.navbar-links {
height: 100%;
}

.navbar-links ul {
display: flex;
margin: 0;
padding: 0;
}

.navbar-links li {
list-style: none;
}

.navbar-links li a {
display: block;
text-decoration: none;
color: white;
padding: 1rem;
}

.navbar-links li:hover {
background-color: #555;
}

.toggle-button {
position: absolute;
top: .75rem;
right: 1rem;
display: none;
flex-direction: column;
justify-content: space-between;
width: 30px;
height: 21px;
}

.toggle-button .bar {
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}

@media (max-width: 800px) {
.navbar {
flex-direction: column;
align-items: flex-start;
}
.toggle-button {
display: flex;
}
.navbar-links {
display: none;
width: 100%;
}
.navbar-links ul {
width: 100%;
flex-direction: column;
}
.navbar-links ul li {
text-align: center;
}
.navbar-links ul li a {
padding: .5rem 1rem;
}
.navbar-links.active {
display: flex;
}
}
<nav class="navbar">
<div class="brand-title">Fruit Basket</div>
<a href="#" class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="navbar-links">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</nav>

最佳答案

将整个内容包裹在 header 中, 设置 max-width: 800px; margin: 0 auto;nav并移动 background-colorheader :

const toggleButton = document.getElementsByClassName('toggle-button')[0]
const navbarLinks = document.getElementsByClassName('navbar-links')[0]

toggleButton.addEventListener('click', () => {
navbarLinks.classList.toggle('active')
})
* {
box-sizing: border-box;
}

body {
margin: 0;
padding: 0;
}

header {
background-color: #333;
}

.navbar {
display: flex;
position: relative;
justify-content: space-between;
align-items: center;
margin: 0 auto;
max-width: 800px;
color: white;
}

.brand-title {
font-size: 1.5rem;
margin: .5rem;
}

.navbar-links {
height: 100%;
}

.navbar-links ul {
display: flex;
margin: 0;
padding: 0;
}

.navbar-links li {
list-style: none;
}

.navbar-links li a {
display: block;
text-decoration: none;
color: white;
padding: 1rem;
}

.navbar-links li:hover {
background-color: #555;
}

.toggle-button {
position: absolute;
top: .75rem;
right: 1rem;
display: none;
flex-direction: column;
justify-content: space-between;
width: 30px;
height: 21px;
}

.toggle-button .bar {
height: 3px;
width: 100%;
background-color: white;
border-radius: 10px;
}

@media (max-width: 800px) {
.navbar {
flex-direction: column;
align-items: flex-start;
}

.toggle-button {
display: flex;
}

.navbar-links {
display: none;
width: 100%;
}

.navbar-links ul {
width: 100%;
flex-direction: column;
}

.navbar-links ul li {
text-align: center;
}

.navbar-links ul li a {
padding: .5rem 1rem;
}

.navbar-links.active {
display: flex;
}
}
<header>
<nav class="navbar">
<div class="brand-title">Fruit Basket</div>
<a href="#" class="toggle-button">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</a>
<div class="navbar-links">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>
</nav>
</header>
margin: 0 auto居中 navmax-width限制它的宽度。如果您希望它更宽,只需更改此属性。

关于javascript - 响应式中心导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62968400/

25 4 0