【问题标题】:Display hamburger menu when mobile else desktop nav bar在移动其他桌面导航栏时显示汉堡菜单
【发布时间】:2017-09-19 20:33:21
【问题描述】:

我正在做移动优先设计,我使用 CSS 和 jQuery 制作了一个我非常喜欢的下拉汉堡菜单,但我想做的是当页面显示在桌面上时让那个汉堡消失,以替换为常规的水平导航栏。我能够使菜单具有响应性,因此至少有一个水平导航栏会在桌面上下拉(而不是像在移动设备上那样长长的菜单列表),但我根本不希望它下拉 - 我希望它完全替换那个汉堡包图标。我不知道 Bootstrap(我是新手),但任何使用 html、css 和 jQuery 的建议都会很棒!谢谢!

这是我的代码:

HTML:

<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="utf-8">
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">google.load("jquery", "1.3.2");</script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

<title>hamburgers</title>
</head>

<body>
    <header>

     <span>Shine Design</span>
     <div id="nav-icon4">
      <span></span>
      <span></span>
      <span></span>
     </div>

    </header>

    <div class="menu">
     <ul>
      <a href="#">
       <li>LINK ONE</li>
      </a>
      <a href="#">
       <li>LINK TWO</li>
      </a>
      <a href="#">
       <li>LINK THREE</li>
      </a>
      <a href="#">
       <li>LINK FOUR</li>
      </a>
      <a href="#">
       <li>LINK FIVE</li>
      </a>
     </ul>
    </div>

  </body>
 </html>

JS:

$(document).ready(function(){
  $('#nav-icon4').click(function(){
    $(this).toggleClass('open');
  });
});

CSS:

 @media only screen and (min-width: 300px) {
  #nav-icon4 {
  width: 35px;
  height: 25px;
  float: right;
  margin-top: 15px;
  margin-right: 30px;
  -webkit-transform: rotate(0deg);
  -moz-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  transform: rotate(0deg);
  -webkit-transition: .5s ease-in-out;
  -moz-transition: .5s ease-in-out;
  -o-transition: .5s ease-in-out;
  transition: .5s ease-in-out;
  cursor: cell; 
 }

#nav-icon4 span {
 display: block;
 position: absolute;
 height: 5px;
 width: 100%;
 background: darkred;
 border-radius: 7px;
 opacity: 2;
 left: 0;
 -webkit-transform: rotate(0deg);
 -moz-transform: rotate(0deg);
 -o-transform: rotate(0deg);
 transform: rotate(0deg);
 -webkit-transition: .25s ease-in-out;
 -moz-transition: .25s ease-in-out;
 -o-transition: .25s ease-in-out;
 transition: .25s ease-in-out;
}

#nav-icon4 span:nth-child(1) {
 top: 0px;
 -webkit-transform-origin: left center;
 -moz-transform-origin: left center;
 -o-transform-origin: left center;
 transform-origin: left center;
}

#nav-icon4 span:nth-child(2) {
 top: 10px;
 -webkit-transform-origin: left center;
 -moz-transform-origin: left center;
 -o-transform-origin: left center;
 transform-origin: left center;
}

#nav-icon4 span:nth-child(3) {
 top: 20px;
 -webkit-transform-origin: left center;
 -moz-transform-origin: left center;
 -o-transform-origin: left center;
 transform-origin: left center;
}

#nav-icon4.open span:nth-child(1) {
 -webkit-transform: rotate(45deg);
 -moz-transform: rotate(45deg);
 -o-transform: rotate(45deg);
 transform: rotate(45deg);
 top: 0;
 left: 6px;
}

#nav-icon4.open span:nth-child(2) {
 width: 0%;
 opacity: 0;
}

#nav-icon4.open span:nth-child(3) {
 -webkit-transform: rotate(-45deg);
 -moz-transform: rotate(-45deg);
 -o-transform: rotate(-45deg);
 transform: rotate(-45deg);
 top: 25px;
 left: 6px;
}

body {
  font-family: 'Noto Sans', sans-serif;
  margin: 0;
  width: 100%;
  height: 100vh;
  background: #ffffff;
   background-color: black;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

header {
  width: 100%;
  background: #ffffff;
  position: fixed;
  height: 60px;
  line-height: 60px;
  border-bottom: 1px solid #dddddd;
  display: inline-block;
  font-size: 2.1em;
  padding-left: 10px;
}

.menu {
  z-index: 1000000;
  display: none;
  font-weight: bold;
  font-size: 1.2em;
  width: 100%;
  background: #f1f1f1;
  position: fixed;
  margin-top: 60px;
  text-align: center;
  color: black;
 }

.menu ul {
  margin: 0;
  padding: 0;
  list-style-type: none;
  list-style-image: none;
  border-top: #dddddd 1px solid;
 }

.menu li {
  display: block;
  padding: 15px 0 15px 0;
  border-bottom: #dddddd 1px solid;
 }

.menu li:hover {
  display: block;
  background: #585858;
  padding: 15px 0 15px 0;
  border-bottom: #dddddd 1px solid;
  cursor: crosshair;
 }

.menu ul li a {
  text-decoration: none;
  margin: 0px;
  color: black;
 }

.menu ul li a:hover {
  color: white;
  text-decoration: none;
 }

.menu a {
  text-decoration: none;
  color: black;
 }

.menu a:hover {
  text-decoration: none;
  color: white;
  }
 }


@media only screen and (min-width: 601px) {    
   header {
    font-size: 1.5em;
 }

   ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
 }

  li {
    float: right;
    margin-left: 15px;
    margin-right: 25px;
 }

  li a {
    display: block;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
 }
 .menu {
    font-size: .8em;
  }
 }

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    如果您是新手,请帮自己一个忙并学习引导程序,它使这种事情变得非常简单,并阻止您重新发明轮子。

    从这里开始...https://getbootstrap.com/examples/navbar/ 和这里:https://getbootstrap.com/components/#navbar

    要执行您想要的操作,您需要设置 CSS 媒体查询以正确显示。如果您坚持自己从了解媒体断点开始,这正是 bootstrap 所做的。

    /* Custom, iPhone Retina */ 
    @media only screen and (min-width : 320px) {
    
    }
    
    /* Extra Small Devices, Phones */ 
    @media only screen and (min-width : 480px) {
    
    }
    
    /* Small Devices, Tablets */
    @media only screen and (min-width : 768px) {
    
    }
    
    /* Medium Devices, Desktops */
    @media only screen and (min-width : 992px) {
    
    }
    
    /* Large Devices, Wide Screens */
    @media only screen and (min-width : 1200px) {
    
    }
    

    【讨论】:

    • 我一定会接受这个建议并做一些阅读,因为我被教导首先编写移动代码。尽管如此,媒体查询还是让我很困惑。我理解这个概念,但有时我无法让它按我想要的方式工作。
    【解决方案2】:

    您需要更新 CSS 以在您希望它可见的断点处显示汉堡按钮。

    目前您已将其设置为显示在(min-width: 300px),但看起来您的导航变成了 601 像素的大屏幕视图。所以你也想让你的汉堡在 601px 时不显示。

    不过,在单独的说明中,我看到您已经构建了您的汉堡包:

     <div id="nav-icon4">
      <span></span>
      <span></span>
      <span></span>
     </div>
    

    我强烈建议您将此代码修改为以下内容:

    <button type="button" id="nav-icon4" aria-label="Menu">
      <span></span>
      <span></span>
      <span></span>
    </button>
    

    通过这样做,键盘用户将可以访问该按钮。

    【讨论】:

    • 谢谢,我会让它可以访问! :)
    • 添加显示无效,这太棒了,对我来说是朝着正确方向迈出的一步,但是如何让我的导航栏显示在它的位置?
    • 您需要将其设置为在您的 min-width 601 媒体查询中显示块。我已经完成了一个快速演示,对您的代码进行了一些小的修改,以向您展示 codepen.io/scottohara/pen/EmyRBj 祝您继续前进
    • 太棒了。我一直在尝试这样做 2 天,它实际上只是几行代码。哇。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2021-10-20
    • 1970-01-01
    • 2021-05-28
    相关资源
    最近更新 更多