【问题标题】:Responsive navigation bar - won't collapse correctly and funky on Drupal响应式导航栏 - 在 Drupal 上不会正确折叠和时髦
【发布时间】:2018-04-14 15:06:57
【问题描述】:

我创建了一个响应式导航栏。我有两个问题。 1)当我在测试页面上缩小屏幕时,所有链接都不会隐藏。 2) 当我将代码插入 Drupal 时,它会将嵌套的链接放在不同的基线上。它不会在测试页面上这样做。我想不出没有按钮来创建响应栏的方法……可能是因为我是初学者。

这是问题enter image description hereenter image description here 和代码的屏幕截图。

<!DOCTYPE html>
<html>
<head>
<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">
<style>
body {margin:0;font-family:Helvetica}

/* Add a background color to the top navigation */
.topnav {
    overflow: hidden;
	background-color: #486D87;
}

/* Style the links inside the navigation bar */
.topnav a {
    float: left;
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.active {
  background-color: #555;
  color: white;
}

/* Hide the link that should open and close the topnav on small screens */
.topnav .icon {
    display: none;
}

/* Dropdown container - needed to position the dropdown content */
.dropdown {
    float: left;
    overflow: hidden;
}

/* Style the dropdown button to fit inside the topnav */
.dropdown .dropbtn {
    font-size: 17px; 
    border: none;
    outline: none;
    color: white;
    padding: 14px 16px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;
}

/* Style the dropdown content (hidden by default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f1f1f1;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}

/* Style the links inside the dropdown */
.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

/* Add a background color on topnav links and the dropdown button on hover */
.topnav a:hover, .dropdown:hover .dropbtn {
    background-color: #BE6A14;
    color: white;
}

/* Add a background color to dropdown links on hover */
.dropdown-content a:hover {
    background-color: #BE6A14;
    color: black;
}

/* Show the dropdown menu when the user moves the mouse over the dropdown button */
.dropdown:hover .dropdown-content {
    display: block;
}

/* 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), .dropdown .dropbtn {
  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 a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
</style>
</head>

<body>
<div class="topnav" id="myTopnav">
	<a href="/" class="active">Home</a>
	<div class="dropdown">
	<button class="dropbtn">About Us</button>
		<div class="dropdown-content">
			<a href="/about">Store Info</a>
			<a href="/history">History</a>
			<a href="/consignment">Consignment</a>
			<a href="/we-buy-used-books">We Buy Used Books</a>
		</div>
	</div>
	<a href="/event">Events</a>
	<a href="/blog">Blog</a>
	<div class="dropdown">
	<button class="dropbtn">Signed Firsts</button>
		<div class="dropdown-content">
			<a href="/signedfirsts">Signed Fiction</a>
			<a href="/square-non-fiction-subscription">Signed Nonfiction</a>
			<a href="/signed-suspense">Signed Suspense</a>
		</div>
	</div>
	<a href="/staff-recommendations">Staff Picks</a>	
	<div class="dropdown">
	<button class="dropbtn">Square Books, Jr.</button>
		<div class="dropdown-content">
			<a href="/junior">Store Info</a>
			<a href="/junior/history">History</a>
			<a href="/junior/storytime">Storytime</a>
		</div>
	</div>
	<div class="dropdown">
	<button class="dropbtn">Gifts</button>
		<div class="dropdown-content">
			<a href="/square-books-gifts">
			<a href="/gift-cards">Gift Cards</a>
		</div>
	</div>
	<a href="/dear-reader-newsletter">Dear Reader</a>
	<a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>

<script>
/* Toggle between adding and removing the "responsive" class to topnav when the user clicks on the icon */
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
</script>
</body>
</html>

【问题讨论】:

  • 您想在小屏幕上隐藏整个蓝色导航栏吗?
  • 不,我想保留蓝条并隐藏除“主页”以外的所有链接
  • 已修复...缺少一些代码。对不起。文本基线仍然存在问题。可能需要问我们的网站霸主(我们和drupal之间的中间人......)
  • 你能更新代码吗,我可以看看这个问题。
  • 已更新。谢谢!

标签: javascript html css drupal


【解决方案1】:

也许这会解决您的用例:

https://jsfiddle.net/5tof6ry0/4/

css 的变化:

.topnav {
  overflow: hidden;
  background-color: #486D87;
  display: flex;
  justify-content: space-between;
}

【讨论】:

  • 不,那没用。我认为这是我正在使用的样式表。我出于另一个原因更改了样式表并注意到问题已解决,但随后我的下拉菜单不起作用。不知道如何让它们使用不同的样式表。
【解决方案2】:

您的代码看起来不错,您只需要使用媒体查询隐藏按钮元素即可。

将其插入到您的 css 中。

@media screen and (max-width: 600px) {
/*Hide button elements*/
.topnav button, 
.topnav a:not(:first-child) 
{display: none;}

【讨论】:

    猜你喜欢
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    相关资源
    最近更新 更多