2 回答

TA貢獻(xiàn)1780條經(jīng)驗 獲得超4個贊
與 W3Schools 示例相比,您的代碼不起作用的原因是因為 CSS 與您的相比針對不同的元素。
基本上你只需要調(diào)整它所說a的位置li(在你的 CSS 中)。
我還必須class="icon"從<a>元素中刪除并將其添加到相關(guān)的<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貢獻(xiàn)1880條經(jīng)驗 獲得超4個贊
你已經(jīng)做了一切,只是有些事情你做錯了他們是:-
你
<ul>, <li>
無緣無故地使用了你本可以只使用<div>
and<a>
。那個漢堡包圖標(biāo)沒有顯示,因為你沒有粘貼他們的 CSS 鏈接 [
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
]。他們使用他們的自定義鏈接來顯示漢堡包圖標(biāo)。我不知道你是如何以及為什么沒有正確地寫你寫的 CSS 文件名
CS File
不是CS File.css
因為你寫的 CSS 內(nèi)容不起作用
那么,這些是我在您的 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;
}
}
現(xiàn)在在 Java Script 中使用 Java Script 一切都是正確的,因此無需編輯它與以前相同:-
function myFunction() {
var x = document.getElementById("navigation");
if (x.className === "navigation-bar") {
x.className += " responsive";
} else {
x.className = "navigation-bar";
}
}
是的!您可以做的一件事是添加過渡效果,過渡效果看起來真的很棒。
希望有幫助。?。。?/p>
添加回答
舉報