【问题标题】:Flexible navbar broken on smaller screens灵活的导航栏在较小的屏幕上损坏
【发布时间】:2018-04-02 13:01:07
【问题描述】:

我正在尝试创建一个在较小屏幕上具有下拉菜单的灵活导航栏。我基于https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_topnav

下拉菜单在较小的屏幕上中断。我在单词上方有图标。因此,它不是“Home Home”,而是有一个房屋图标,而不是第一个带有标题的房屋。

//  FUNCTION FOR NAV BAR  //
function myfunction() {
  var topNavX = document.getElementById("myTopnav");
  if (topNavX.className === "topnav") {
    topNavX.className += "responsive";
  } else {
    topNavX.className = "topnav";
  }
}
.topnav {
	background-color: #66FCF1;
	overflow: auto;
	display: flex;
	margin-top: 15px;
	margin-left: 2%;
	margin-right: 2%;
	border-radius: 10px;
	text-align: justify;

}
.topnav:after{
	content: '';
}
/* Style the links inside the navigation bar */
.topnav a {
	float: left;
	display: block;
	color: black;
	text-align: center;
	padding: 14px 16px;
	text-decoration: none;
	font-size: 17px;
}
/* When the screen is less than 600 pixels wide, hide all links, except for the first one ("Home"). Show the link that contains should open and close the topnav (.icon) */
@media screen and (max-width: 600px) {
	.topnav a:not(:first-child) {display: none;}
	.topnav a.icon {
		float: right;
		display: block;
	}
}
/* The "responsive" class is added to the topnav with JavaScript when the user clicks on the icon. This class makes the topnav look good on small screens (display the links vertically instead of horizontally) */
@media screen and (max-width: 600px) {
	.topnav.responsive {position: relative;}
	.topnav.responsive .iconJS {
		position: absolute;
		right: 0;
		top: 0;
	}
	.topnav.responsive a {
		float: none;
		display: block;
		text-align: left;
	}
}
                            /*  ABOUT ON TOP NAV BAR  */
#topNavAbout {
	flex: 1;
	font-family: "Walter Turncoat";
	font-weight: bold;
	line-height: 160%;
}
                        /*  ABOUT ON TOP NAV BAR ON HOVER  */
#topNavAbout:hover {
	background-color: darkgoldenrod;
}
                             /*  A TO Z ON TOP NAV  */
#topNavAlpha {
	flex: 1;
	font-family: "Cinzel Decorative";
	font-weight: bold;
	line-height: 160%;
}
                         /*  A TO Z ON TOP NAV ON HOVER  */
#topNavAlpha:hover {
	background-color: darkviolet;
}
                      /*  FLEX AND FONT FOR CHEESY IN NAV BAR  */
#topNavCheesy {
	flex: 1;
	font-family: "Bad Script";
	font-weight: 900;
	line-height: 160%;
	font-size: 1.1em;
}
                     /*  TOP NAV CHEESY ON HOVER  */
#topNavCheesy:hover{
	background-color: #FF0;
}
                  /*  CLASSIC ON TOP NAV BAR  */
#topNavClassic {
	flex: 1;
	font-family: "Sofia";
	font-weight: bold;
	font-size: 1.3em;
	line-height: 150%;
}
                  /*  CLASSIC ON TOP NAV BAR ON HOVER  */
#topNavClassic:hover {
	background-color: burlywood;
}
#topNavDonate {
	flex: 1;
	font-family: "Verdana";
	font-weight: bold;
	line-height: 170%;
}
#topNavDonate:hover {
	background-color: #009CDE;
}
                       /*  CHANGED TEXT ON MAIN NAV BAR  */  
#topNavFav {
	font-family: "Permanent Marker";
	flex: 1;
	line-height: 160%;
}
                         /*  FAVORITE HOVER ON NAV BAR  */
#topNavFav:hover {
	background-color: #00FF00;
}
                            /*  GAME ON TOP NAV  */
#topNavGame {
	flex: 1;
	font-family: "Monoton";
	font-weight: bold;
}
                         /*  GAME ON TOP NAV ON HOVER  */
#topNavGame:hover {
	background-color: orange; 
}
                       /*  CHANGED TEXT ON MAIN NAV BAR  */
#topNavGod {
	flex: 1;
	font-family: "Finger Paint";
	font-size: .86em;
	font-weight: bold;
	line-height: 190%;
}
                    /*  GOD-AWFUL ON HOVER OVER MAIN NAV BAR  */
#topNavGod:hover {
	background-color: white;
}
                               /*  NAV BAR GROSS  */
#topNavGross {
	flex: 1;
	font-family: "Nosifer";
}
                       /*  GROSS ON MAIN NAV BAR ON HOVER  */
#topNavGross:hover {
	background-color: #F00;
}
                              /*  Home ICON and TEXT */
.topNavHome {
	flex: 1;
	font-family: "Sedgwick Ave";
	font-weight: bold;
	line-height: 150%;
}
                            /*  ROULETTE ON TOP NAV  */
#topNavRoul {
	flex: 1;
	font-family: "Marck Script";
	font-weight: bold;
	font-size: 1.5em;
}
                           /*  ROULETTE ON TOP NAV ON HOVER  */
#topNavRoul:hover {
	background-color: crimson;
}
                              /*  WTF ON MAIN NAV BAR  */
#topNavWTF {
	flex: 1;
	font-family: "Rock Salt";
	font-size: .75em;
	font-weight: bold;
}
                         /*  WTF ON MAIN NAV BAR ON HOVER  */
#topNavWTF:hover {
	background-color: fuchsia;
}
                            <!--  MAIN NAV BAR -->
<div class="topnav" id="myTopnav">
  <a class="topNavHome" id="current0" href="https://www.alansmitheepicks.com"><img src="icons/home.png" title="Home" alt="Home"><br>Home</a>
  <a id="topNavFav" href="favorites.html"><img src="icons/fav.png" title="Favorites" alt="Favorites"><br>Favorites</a>
  <a id="topNavCheesy" href="cheesy.html"><img src="icons/cheese.png" title="Cheesy" alt="Cheesy"><br>Cheesy</a>
  <a id="topNavGod" href="godawful.html"><img src="icons/godawful.png" title="God Awful" alt="God Awful"><br>Godawful</a>
  <a id="topNavWTF" href="wtf.html"><img src="icons/wtf.png" title="WTF" alt="WTF?!"><br>W...T...F?!</a>
  <a id="topNavGross" href="gross.html"><img src="icons/gross.png" title="Gross" alt="Gross"><br>Gross</a>
  <a id="topNavClassic" href="classic.html"><img src="icons/classic.png" title="Classic" alt="Classics"><br>Classic</a>
  <a id="topNavAlpha" href="atoz.html"><img src="icons/atoz.png" title="A to Z" alt="Search A-Z"><br>A to Z</a>
  <a id="topNavGame" href="game.html"><img src="icons/game.png" title="Game" alt="Game"><br>Game</a>
  <a id="topNavRoul" href="roulette.html"><img src="icons/roulette.png" title="Roulette" alt="Roulette"><br>Roulette</a>
  <a id="topNavDonate" href="donate.html"><img src="icons/paypal.png" title="Donate" alt="Donate"><br>Donate</a>
  <a id="topNavAbout" href="about.html"><img src="icons/clapper.png" title="About Us" alt="Who is Alan Smithee?"><br>About</a>
  <a href="#" class="icon" onclick=myFunction()>&#9776;</a>
</div>

【问题讨论】:

  • 您好,欢迎来到 SO.. 您的问题是?请参考How do I ask a good question?
  • 下拉菜单中断”并没有告诉我们太多——到底发生了什么?它不显示吗?会影响下面的内容吗?它是否显示不正确,如果是,它应该如何显示以及它实际上是如何显示的?

标签: javascript html css


【解决方案1】:

附加一个额外的类

topNavX.className += "响应式";

在追加新的类名之前应该有一个空格。

【讨论】:

    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 2015-02-28
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多