【发布时间】:2021-08-30 13:33:20
【问题描述】:
我正在尝试制作一个导航栏,其中一个类别的颜色(蓝色)与其他类别(绿色)不同。当您将鼠标悬停在此蓝色 HOME 类别之外时,它需要变为绿色。悬停的类别应变为蓝色,然后在未悬停时返回绿色。以下是一些图片供参考:
默认/主页状态:用户从主页开始。 Home 类别是蓝色的,以向用户显示他们所在的位置。
雇主状态:用户已导航到雇主页面。 Home 类别现在是绿色的。
我在 SFMC 工作,所以我必须在一个集合中包含所有 HTML、CSS 和 Javascript。这是我的代码:
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
.body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #47a23f;
}
.topnav a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.topnav a:hover,
.dropdown:hover .dropbtn {
background-color: #005da6;
}
.topnav a.active {
background-color: #005da6;
color: white;
}
.topnav .icon {
display: none;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #47a23f;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: #fff;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #003150;
color: #fff;
}
.dropdown:hover .dropdown-content {
display: block;
color: #fff;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {
display: none;
}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {
position: relative;
}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: left;
}
}
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Home</a>
<div class="dropdown">
<button class="dropbtn">Workforce
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a target="blank" href="https://myactivehealth.com/Portal/PreRegistrations/Index">Request to enroll</a>
<a target="blank" href="https://www.myactivehealth.com/Portal/Registration/RegistrationStep1?IsoAuthDPRequest=False">Create an account</a>
<a target="blank" href="https://www.myactivehealth.com/Portal/PortalLogin.aspx?SupplierURL=15571">Log in</a>
</div>
</div>
<a href="#contact">Employer</a>
<div class="dropdown">
<button class="dropbtn">Injured Workers
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a target="blank" href="https://www.myactivehealth.com/Portal/Registration/RegistrationStep1?IsoAuthDPRequest=False">Create an account</a>
<a target="blank" href="https://www.myactivehealth.com/Portal/PortalLogin.aspx?SupplierURL=15571">Log in</a>
</div>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
【问题讨论】:
-
问题是什么?它似乎在做你想做的事还是你想让它在点击后保持粘性?
-
正如@mplungjan 指出的那样,悬停使颜色变化工作正常。我怀疑您的问题是如何在实际输入另一个页面时使相关选项卡变为蓝色。是这样吗?
-
@mplungjan 有什么问题
标签: javascript html css navigation