【问题标题】:How can I make my existing responsive navigation bar into a hamburger menu for smaller screens?如何将现有的响应式导航栏变成小屏幕的汉堡菜单?
【发布时间】:2018-05-03 13:18:14
【问题描述】:

我设计了一个响应式导航栏,到目前为止我真的很喜欢它的样式。但是,尽管它本质上是响应式的,但在最小化分辨率时它不会创建汉堡菜单。有没有办法将代码添加到我现有的导航栏以创建这种效果(不会丢失我的样式)?抱歉,如果我的代码有点草率。任何帮助将非常感激。

<!doctype html>
<!--[if lt IE 7]> <html class="ie6 oldie"> <![endif]-->
<!--[if IE 7]>    <html class="ie7 oldie"> <![endif]-->
<!--[if IE 8]>    <html class="ie8 oldie"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Untitled Document</title>
<link href="boilerplate.css" rel="stylesheet" type="text/css">
<link href="index.html" rel="stylesheet" type="text/css">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="respond.min.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css"/>
<style type="text/css">
body,td,th {
color: #CCC;
}
body {
background-image: url(images/bg-main.jpg);
}
</style>
</head>
<body>
<div class="feed">
<img src="images/bg-nav.png" class="trw" alt="header" longdesc="http://roguewarriorstc.com"> </div>
<div id="LayoutDiv1">
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Home</a>
<a href="#about">About</a>
<a href="#contact">Online Dojo</a>
<a href="#tenshin">TenShin Aikido</a>
<a href="#tsaa">TSAA</a>
<a href="#events">Live Events</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#forum">Forum</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">&#9776;</a>
</div> 

</div>

</body>
</html>




@charset "utf-8";
/* CSS Document */
/* Show in default resolution screen*/

/* Add a black background color to the top navigation */
#LayoutDiv1 {
max-width: 80%;
margin-left: auto;
margin-right: auto;
-moz-border-image: url(images/bg-main.jpg) 0;
}

.topnav {
border-radius: 10px;
background-image:url(images/bg-nav.jpg);
max-width: 100%;
text-align: center;
background-color: #333;
overflow: hidden;
border-style: solid; color:#666;
border-width: thin;

}

/* Style the links inside the navigation bar */
.topnav a {
float: left;
display: block;
font-weight: bold;
color: #f2f2f2;
text-align: center;
padding: 12px 16px;
text-decoration: none;
font-size: 13px;
}

/* Change the color of links on hover */
 .topnav a:hover {
background-color: #00C;
opacity: 0.6;
color: white;
}

/* Add an active class to highlight the current page */
.active {
background-color: #00C;
opacity: 0.6;
color: black;
}

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

 img.trw
{
position: relative;
max-width: 70%;
margin: 0;
padding: 0;
padding-bottom: 0px;
}

.feed {
border-radius: 10px 10px 0px 0px;
background-image:url(images/bg-nav.jpg);

text-align: center;
font-color: #00C; 
font-weight:bold;
background-color: #333;
    overflow: hidden;
max-width: 70%;
margin-left: auto;
margin-right: auto;
padding: 0px;
border-style: solid; color:#666;
border-bottom: none;
border-width: thin;
-moz-border-image: url(images/bg-h2.jpg) 0;
margin-top: 10px;

    }

【问题讨论】:

  • 可以尝试学习css查询
  • 欢迎来到 SO。我冒昧地稍微编辑了您的问题。我嵌入了图片并删除了你的介绍,因为问候、感谢、个人介绍等被认为会分散实际问题的注意力。
  • 谢谢。我确实尝试添加图片,但它说我没有足够的帖子来做。感谢您的帮助。

标签: html css navigation nav


【解决方案1】:

您可以使用 CSS 媒体查询和 JavaScript 来完成此操作。

使用 CSS(推荐):

在您的 CSS 中创建媒体查询,这是一种基于屏幕宽度创建响应式 CSS 规则的方法。见这篇文章: https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries, 标题为: 创建复杂的媒体查询

这是 CSS 的工作代码 sn-p,点击运行代码 sn-p,然后调整窗口的宽度,直到导航按钮 div 消失并出现汉堡包

<!DOCTYPE html>
<html>

<head>
  <style>
    @media only screen and (min-width: 701px) {
      /* hide the hamburger */
      .hamburger {
        display: none;
      }
      /* show all the nav-buttons */
      .nav-button {
        display: block;
      }
    }
    /* if browser window is small (max-width) */
    
    @media only screen and (max-width: 700px) {
      /* show everything with the hamburger class */
      .hamburger {
        display: block;
      }
      /* hide all the nav-buttons */
      .nav-button {
        display: none;
      }
    }
  </style>
</head>

<body>
  <div class="hamburger">
    hamburger
  </div>

  <div class="nav-button">
    nav-button
  </div>

  <div class="nav-button">
    nav-button
  </div>

  <div class="nav-button">
    nav-button
  </div>

  <div class="nav-button">
    nav-button
  </div>
</body>

</html>

使用 JavaScript:

你想使用 window.matchMedia 函数: https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia

if (window.matchMedia("(min-width: 400px)").matches) {
  // make all the navigation visible
  // hide the hamburger icon
} else {
  // show the hamburger icon
  // hide the navigation until someone presses the hamburger
}

【讨论】:

  • 谢谢凯尔。我会先尝试css。在您的代码中,带有 3 个点的花括号是什么?那是代表汉堡包,还是代表更多代码的占位符?
  • @Lori,“...”只是一个占位符,我添加了一个更明显的 CSS 版本。我同意你的观点,我认为 CSS 是更好的选择。
  • 啊哈,谢谢凯尔。我会将它添加到我的代码中,并希望它有效。我是否必须在任何特定的地方添加它,或者它可以在 css 文档中的任何地方?对不起,我是新手。非常感谢您的帮助。
  • @Lori,实际上我确实认为您需要将 CSS 放在特定的地方才能使其工作,我会对媒体查询进行一些研究,因为它是一个很大的话题。我通常在谷歌上搜索“MDN 媒体查询”,因为 MDN 通常是一个非常好的学习网站(不过这只是我)
  • 它不起作用。我创建了一个视频截图向您展示,但我不知道如何发布它以便您可以看到它的实际效果。
猜你喜欢
  • 2021-06-21
  • 2018-05-10
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多