gpt4 book ai didi

javascript - 切换导航栏未正确切换

转载 作者:行者123 更新时间:2023-11-28 00:17:22 27 4
gpt4 key购买 nike

我刚刚创建了一个响应式切换导航栏。一切正常,只有一个问题是它没有按应有的方式正确切换。单击鼠标时可以看到下拉菜单,一旦松开鼠标,下拉菜单就会消失(引用http://testpage187.tumblr.com/)。它发生在桌面和移动设备上(因为它是响应式的,我认为它对两者都一样有效)。我认为问题出在javascript代码上。请调查一下。我在这里附上我的代码。提前谢谢你。

$(document).ready(function() {
$('.menu-toggle').click(function() {
$('nav').toggleClass('active')
})
$('ul li').click(function() {
$(this).siblings().removeClass('active');
$(this).toggleClass('active');
})
})
  body {
margin: 0;
padding: 0;
width: 100%;
font-family: sans-serif;
}

header {
position: absolute;
top: 50px;
left: 0px;
padding: 0 100px;
background: #262626;
width: 100%;
box-sizing: border-box;
}

header .logo {
color: #fff;
float: left;
height: 50px;
line-height: 50px;
font-size: 24px;
font-weight: bold;
}

header nav {
float: right;
}

header nav ul {
margin: 0;
padding: 0;
display: block;
}

header nav ul li {
list-style: none;
display: inline;
position: relative;
}

header nav ul li.sub-menu:before {
content: '\f0d7';
font-family: 'fontAwesome';
position: absolute;
line-height: 50px;
color: #fff;
right: 5px;
}

header nav ul li.active.sub-menu:before {
content: '\f0d8';
}

header nav ul li ul {
position: absolute;
left: 0;
top: 33px;
background: #333;
display: none;
}

header nav ul li:active ul {
display: block;
}

header nav ul li ul li {
display: block;
width: 200px;
}

header nav ul li ul li a {
display: inline-block;
}

header nav ul li a {
height: 50px;
line-height: 50px;
padding: 0 20px;
color: #fff;
text-decoration: none;
display: inline-block;
}

header nav ul li a:hover,
header nav ul li a:active {
background: #2196f3;
}

.menu-toggle {
color: #fff;
float: right;
line-height: 50px;
font-size: 24px;
cursor: pointer;
display: none;
}

@media screen and (max-width: 800px) {
header {
padding: 0 20px;
}
.menu-toggle {
display: block;
}
header nav {
position: absolute;
width: 100%;
height: calc(100vh - 50px);
background: #333;
top: 50px;
left: -100%;
transition: 0.5s;
}
header nav.active {
left: 0;
top: 50px;
}
header nav ul {
width: 100%;
display: block;
text-align: center;
}
header nav ul li {
width: 100%;
display: block;
border-bottom: 1px solid rgba(0, 0, 0, .2);
}
header nav ul li a {
display: block;
}
header nav ul li.active ul {
position: relative;
background: #003e6f;
}
header nav ul li ul li {
width: 100%;
text-align: center;
}
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<header>
<div class="logo">LOGO</div>
<nav>
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">Trending</a></li>
<li><a href="#">Popular</a></li>
<li><a href="#" class="sub-menu">New <i class="fas fa-caret-down"></i></a>
<ul>
<li><a href="#">New 1</a></li>
<li><a href="#">New 2</a></li>
<li><a href="#">New 3</a></li>
<li><a href="#">New 4</a></li>
<li><a href="#">New 5</a></li>
</ul>
</li>
<li><a href="" class="sub-menu">Contact <i class="fas fa-caret-down"></i></a>
<ul>
<li><a href="#">Contact 1</a></li>
<li><a href="#">Contact 2</a></li>
<li><a href="#">Contact 3</a></li>
<li><a href="#">Contact 4</a></li>
</ul>
</li>
<li><a href="#">About</a></li>
</ul>
</nav>
<div class="menu-toggle"><i class="fa fa-bars" aria-hidden="true"></i></div>

最佳答案

解决方法如下:

您的 CSS 有问题。您在 sibling 上添加了一个“状态”:active 而不是类 .active

CSS 问题:

header nav ul li a:hover,
header nav ul li a:active {
background:#2196f3;
}

header nav ul li:active ul{
display:block;
}

CSS 解决方案:

header nav ul li a:hover,
header nav ul li a.active {
background:#2196f3;
}

header nav ul li.active ul{
display:block;
}

我还修复了一个top位移。

这是 fiddle :Toggle solution

关于javascript - 切换导航栏未正确切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55030529/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com