如何解决在 onclick javascript 效果之后,我可以在 css 中定位一个 :hover 函数吗?
因此,当我将鼠标悬停在菜单上时,背景变为红色,文本变为白色。 当我单击下拉链接时,将显示第二个 UL。 我希望在单击它时立即定位 LI 项目 .dropdown:hover。并将其设置为不悬停。
这是用 css 还是 javascript 完成的? 我一直在尝试使用两者,但似乎无法针对正确的 li。
let menuBtn = document.querySelector(".menu-Btn");
let menu = document.querySelector(".mainMenu");
// Button toggle-menu
menuBtn.onclick = ()=>{
if (menu.classList.contains("show")){
menu.classList.remove("show");
} else {
menu.classList.add("show");
}
}
// Dropdown menu
let toggle = document.querySelector(".dropdown");
let sub = document.querySelector(".submenu");
toggle.onclick = ()=>{
if (sub.classList.contains("show")){
sub.classList.remove("show");
} else {
sub.classList.add("show");
}
}
* {
margin: 0;
padding: 0;
}
body {
background: #b3b3b3;
justify-content: center;
font-family: 'Open Sans',sans-serif;
}
header {
margin: 0 0 20px 0;
padding: 15px 0;
width: 100%;
height: auto;
background: rgb(255,255,255);
}
.headerContainer,.container {
width: 90%;
margin-left: auto;
margin-right: auto;
padding: 15px;
border: 1px solid #000;
}
.headerContainer {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
}
.headerBrand a {
font-size: 30px;
font-weight: 800;
text-decoration: none;
color: rgb(0,0);
display: flex;
align-items: center;
}
.headerBrand a img {
padding: 0 10px 0 0;
}
header nav {
top: 100%;
left: 0;
width: 100%;
position: absolute;
max-height: 70vh;
overflow-y: auto;
}
header nav::-webkit-scrollbar {width: 10px;}
header nav::-webkit-scrollbar-track {background: #f1f1f1;}
header nav::-webkit-scrollbar-thumb {background: #888; }
header nav::-webkit-scrollbar-thumb:hover {background: #555;}
header nav .mainMenu {
padding: 15px;
display: none;
list-style: none;
background: #ffffff;
border-radius: 5px;
}
header nav .mainMenu.show {
display: block;
}
header nav ul li {
padding: 5px 0;
background:rgb(255,255);
border-bottom:1px solid rgb(179,179,179);
position: relative;
justify-content: space-between;
}
header nav ul li:last-child {
border: none;
}
header nav ul li a {
display: block;
font-size: 15px;
font-weight: 700;
width: calc(100% - 10px);
text-transform: uppercase;
color: red;
text-decoration: none;
padding: 0 0 0 15px;
}
header nav ul li.activepage {
background: red;
border-radius: 5px 5px 0 0;
}
header nav ul li.activepage a {
color: white;
}
header nav ul li.dropdown a i {
top: 10px;
right: 10px;
font-size: 10px;
position: absolute;
}
header nav .mainMenu .submenu {
display: none;
padding: 0 0 0 15px;
position: relative;
border-radius: 0%;
}
header nav .mainMenu .submenu.show {
display: block;
}
header nav .mainMenu li .submenu li {
padding: 5px 0 5px 0;
}
header nav .mainMenu li .submenu li a {
text-transform: none;
font-weight: 400;
}
.menu-Btn {
width: 35px;
height: auto;
text-align: center;
background: red;
font-size: 25px;
border-radius: 5px;
cursor: pointer;
}
.menu-Btn i {
color: white;
}
header nav ul li:hover {
background: red;
border-radius: 5px;
}
header nav ul li:hover a {
color: white;
}
<head>
<script src="https://kit.fontawesome.com/a9b20b17fc.js" crossorigin="anonymous"></script>
</head>
<header>
<div class="headerContainer">
<div class="headerBrand">
<a href="#">logo</a>
</div>
<nav>
<ul class="mainMenu">
<li class="activepage"><a href="#">Home</a></li>
<li class="dropdown">
<a href="#">Dropdown<i class="fas fa-plus"></i></a>
<ul class="submenu">
<li><a href="#">some link</a></li>
<li><a href="#">some link</a></li>
<li><a href="#">some link</a></li>
</ul>
</li>
<li><a href="#">Another link</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<div class="menu-Btn"><i class="fas fa-bars"></i></div>
</div>
</header>
<!-- END header -->
<div class="container">
<section>
test
</section>
</div>
解决方法
通过使用 javascript 添加活动类来修复它
let menuBtn = document.querySelector(".menu-Btn");
let menu = document.querySelector(".mainMenu");
// Button toggle-menu
menuBtn.onclick = ()=>{
if (menu.classList.contains("show")){
menu.classList.remove("show");
} else {
menu.classList.add("show");
}
}
// Dropdown menu
let toggle = document.querySelector(".dropdown");
let sub = document.querySelector(".submenu");
toggle.onclick = ()=>{
if (sub.classList.contains("show")){
sub.classList.remove("show");
toggle.classList.remove("active");
} else {
sub.classList.add("show");
toggle.classList.add("active");
}
}
* {
margin: 0;
padding: 0;
}
body {
background: #b3b3b3;
justify-content: center;
font-family: 'Open Sans',sans-serif;
}
header {
margin: 0 0 20px 0;
padding: 15px 0;
width: 100%;
height: auto;
background: rgb(255,255,255);
}
.headerContainer,.container {
width: 90%;
margin-left: auto;
margin-right: auto;
padding: 15px;
border: 1px solid #000;
}
.headerContainer {
position: relative;
display: flex;
justify-content: space-between;
align-items: center;
}
.headerBrand a {
font-size: 30px;
font-weight: 800;
text-decoration: none;
color: rgb(0,0);
display: flex;
align-items: center;
}
.headerBrand a img {
padding: 0 10px 0 0;
}
header nav {
top: 100%;
left: 0;
width: 100%;
position: absolute;
max-height: 70vh;
overflow-y: auto;
}
header nav::-webkit-scrollbar {width: 10px;}
header nav::-webkit-scrollbar-track {background: #f1f1f1;}
header nav::-webkit-scrollbar-thumb {background: #888; }
header nav::-webkit-scrollbar-thumb:hover {background: #555;}
header nav .mainMenu {
padding: 15px;
display: none;
list-style: none;
background: #ffffff;
border-radius: 5px;
}
header nav .mainMenu.show {
display: block;
}
header nav ul li {
padding: 5px 0;
background:rgb(255,255);
border-bottom:1px solid rgb(179,179,179);
position: relative;
justify-content: space-between;
}
header nav ul li:last-child {
border: none;
}
header nav ul li a {
display: block;
font-size: 15px;
font-weight: 700;
width: calc(100% - 10px);
text-transform: uppercase;
color: red;
text-decoration: none;
padding: 0 0 0 15px;
}
header nav ul li.activepage {
background: red;
border-radius: 5px 5px 0 0;
}
header nav ul li.activepage a {
color: white;
}
header nav ul li.dropdown a i {
top: 10px;
right: 10px;
font-size: 10px;
position: absolute;
}
header nav .mainMenu .submenu {
display: none;
padding: 0 0 0 15px;
position: relative;
border-radius: 0%;
}
header nav .mainMenu .submenu.show {
display: block;
}
header nav .mainMenu li .submenu li {
padding: 5px 0 5px 0;
}
header nav .mainMenu li .submenu li a {
text-transform: none;
font-weight: 400;
}
.menu-Btn {
width: 35px;
height: auto;
text-align: center;
background: red;
font-size: 25px;
border-radius: 5px;
cursor: pointer;
}
.menu-Btn i {
color: white;
}
header nav ul li:not(.active):hover {
background: red;
border-radius: 5px;
}
header nav ul li:not(.active):hover a {
color: white;
}
<head>
<script src="https://kit.fontawesome.com/a9b20b17fc.js" crossorigin="anonymous"></script>
</head>
<body>
<!-- header -->
<header>
<div class="headerContainer">
<div class="headerBrand">
<a href="#">Logo</a>
</div>
<nav>
<ul class="mainMenu">
<li class="activepage"><a href="#">Home</a></li>
<li class="dropdown">
<a href="#">DROPDOWN<i class="fas fa-plus"></i></a>
<ul class="submenu">
<li><a href="#">another link</a></li>
<li><a href="#">another link</a></li>
<li><a href="#">Manother link</a></li>
</ul>
</li>
<li><a href="#">another link</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<div class="menu-Btn"><i class="fas fa-bars"></i></div>
</div>
</header>
<!-- END header -->
<div class="container">
<section>
test
</section>
</div>
</body>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。