2 回答
TA贡献1780条经验 获得超3个赞
与 W3Schools 示例相比,您的代码不起作用的原因是因为 CSS 与您的相比针对不同的元素。
基本上你只需要调整它所说a的位置li(在你的 CSS 中)。
我还必须class="icon"从<a>元素中删除并将其添加到相关的<li>(在 HTML 中)。
请参阅下面的示例;
function myFunction() {
var x = document.getElementById("navigation");
if (x.className === "navigation-bar") {
x.className += " responsive";
} else {
x.className = "navigation-bar";
}
}
.navigation-bar{
width:100%;
list-style-type: none;
margin:0;
padding:0;
background-color: #DDDDDD;
overflow:hidden;
}
.navigation-bar li{
display:inline;
}
.navigation-bar li{
text-decoration: none;
font-size: 120%;
color: #000000;
padding-left:1%;
padding-right:1%;
padding-top:0.5%;
padding-bottom:0.5%;
display:inline-block;
}
.navigation-bar li:hover{
background-color:#999999;
}
.navigation-bar li.active{
background-color: #999999;
}
.navigation-bar .icon{
display:none;
}
@media screen and (max-width: 900px){
.navigation-bar li:not(:first-child) {display: none;}
.navigation-bar li.icon{
float:right;
display: block;
}
}
@media screen and (max-width: 900px) {
.navigation-bar.responsive {position: relative;}
.navigation-bar.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
.navigation-bar.responsive li {
float: none;
display: block;
text-align: left;
}
}
<html>
<head>
<meta charset = "utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport" />
<title>Title</title>
<link rel=stylesheet type="text/css" href="CS File">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<h1><a href="index.html">Website Title</a></h1>
<ul id="navigation" class="navigation-bar">
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 6</a></li>
<li><a href="#">Item 7</a></li>
<li class="icon"><a href="javascript:void(0);" onclick="myFunction()"><i class="fa fa-bars"></i></a></li>
</ul>
<script type="text/javascript" src="JS File"></script>
<script type="text/javascript" src="css3-mediaqueries.js"></script>
</body>
</html>
TA贡献1880条经验 获得超4个赞
你已经做了一切,只是有些事情你做错了他们是:-
你
<ul>, <li>
无缘无故地使用了你本可以只使用<div>
and<a>
。那个汉堡包图标没有显示,因为你没有粘贴他们的 CSS 链接 [
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
]。他们使用他们的自定义链接来显示汉堡包图标。我不知道你是如何以及为什么没有正确地写你写的 CSS 文件名
CS File
不是CS File.css
因为你写的 CSS 内容不起作用
那么,这些是我在您的 HTML 文件中所做的更改:-
<html>
<head>
<meta charset = "utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta content="width=device-width, initial-scale=1" name="viewport" />
<title>Title</title>
<link rel=stylesheet type="text/css" href="CS File.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> </head>
<body>
<h1><a href="index.html">Website Title</a></h1>
<div id="navigation" class="navigation-bar">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
<a href="#">Item 4</a>
<a href="#">Item 5</a>
<a href="#">Item 6</a>
<a href="#">Item 7</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()"><i class="fa fa-bars"></i></a>
</div>
</body>
</html>
这是你的 CSS 代码,我稍微编辑了一下
/* ... Additional CSS Settings... */
.navigation-bar{
width:100%;
list-style-type: none;
margin:0;
padding:0;
background-color: #DDDDDD;
overflow:hidden;
}
.navigation-bar a{
display:inline;
text-decoration: none;
font-size: 120%;
color: #000000;
padding-left:1%;
padding-right:1%;
padding-top:0.5%;
padding-bottom:0.5%;
display:inline-block;
}
.navigation-bar a:hover{
background-color:#999999;
}
.navigation-bar a.active{
background-color: #999999;
}
.navigation-bar .icon{
display:none;
}
/* ... Additional CSS Settings.. */
@media screen and (max-width: 900px){
.navigation-bar a:not(:first-child) {display: none;}
.navigation-bar a.icon{
float:right;
display: block;
}
}
@media screen and (max-width: 900px) {
.navigation-bar.responsive {position: relative;}
.navigation-bar.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.navigation-bar.responsive a {
float: none;
display: block;
text-align: left;
}
}
现在在 Java Script 中使用 Java Script 一切都是正确的,因此无需编辑它与以前相同:-
function myFunction() {
var x = document.getElementById("navigation");
if (x.className === "navigation-bar") {
x.className += " responsive";
} else {
x.className = "navigation-bar";
}
}
是的!您可以做的一件事是添加过渡效果,过渡效果看起来真的很棒。
希望有帮助。!!!
添加回答
举报