【发布时间】:2019-02-27 03:08:31
【问题描述】:
我从w3schools 复制了这个菜单。我已经编辑了一些东西(将三个链接浮动到右侧,并更改了一些颜色)。
桌面视图:
手机端查看:
但是,您可以看到项目链接显示在移动视图中,这不是我想要的。如何禁用该项目链接?我已经在 CSS 中尝试过一些类似的东西::not(:first-child)。
这是我的代码:
function jsnav() {
var x = document.getElementById("js-nav");
if (x.className === "nav") {
x.className += " responsive";
} else {
x.className = "nav";
}
}
body {
margin: 0;
padding: 0;
font-family: "helvetica neue", sans-serif;
}
nav {
overflow: hidden;
background-color: #333;
}
nav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
nav a:hover {
color: white;
}
.active {
background-color: dodgerblue;
color: white;
}
nav .icon {
display: none;
}
.float-right {
float: right;
}
@media screen and (max-width: 600px) {
nav a:not(:first-child) {display: none;}
nav a.icon {
float: right;
display: block;
padding-top: 10px;
}
.float-right {
float: left;
}
}
@media screen and (max-width: 600px) {
nav.responsive {position: relative;}
nav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
nav.responsive a {
float: none;
display: block;
text-align: left;
}
}
main {
padding: 40px 40px 20px 80px;
}
@media only screen and (max-width: 800px){
main {
padding-left: 40px;
padding-right: 40px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- CSS -->
<link rel="stylesheet" href="css/style.css">
<title>CSS Nav HTML & CSS JS</title>
</head>
<body>
<nav id="js-nav">
<a href="#" class="active">Home</a>
<div class="float-right">
<a href="#">Projects</a>
<a href="#">About</a>
<a href="#">Contact</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="jsnav()">
☰
</a>
</nav>
<main>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</main>
<script src="js/navigation.js"></script>
</body>
</html>
【问题讨论】:
-
所以你想隐藏那个项目链接?所以
float-right的所有链接都应该被隐藏? -
是的,这是正确的,只有当汉堡按钮被触摸时,浮动右链接应该是可见的
标签: javascript html css css-selectors