简体   繁体   中英

Make a navbar turn into a Hamburger push-out/slide-out menu when the screen gets too small

Welp... I'm stuck... again...

(FYI This is my first ever website that I am coding myself (not a template) so don't assume that I know anything.)

I am in the process of making my homepage more mobile / smaller screen sizes freindly. One of the biggest problems is that my navbar is too large on mobile and makes it so that in order to see the full navbar you have to scroll to the right, and this really breaks the website aesthetically. I have tried my best to implement things like flexnav into my website but I haven't been able to succeed in making it:

A. Be a Slide-Out / Push-Out menu on mobile which is triggered by a hamburger menu (which is preferably animated like style 2 in this tutorial: http://callmenick.com/post/animating-css-only-hamburger-menu-icons )

B. Have a TRANSPARENT background for both the hamburger menu and the navbar. B1. Keep my navbar's color-scheme

C. Switch from the full nav-bar to the hamburger menu when the screen is less than 1200 px wide.

I know that I am asking a lot, but hopefully someone will be able to help me. As I am submitting my code in JSBin it would be really helpful If you could do the same (or if you prefer Codepen or JSFiddle or whatever). And I am sure that this stylish approach to a navbar which adapts to screen size would be appreciated by many people who find themselves in the same situation I do. Also, sorry for the messy code.

JSFBin: http://jsbin.com/jefosiweci/edit?html,css,js,output

Code:

    <meta name="robots" content="noindex">
  <head>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha256-KXn5puMvxCw+dAYznun+drMdG1IFl3agK0p/pqT9KAo= sha512-2e8qq0ETcfWRI4HJBzQiA3UoyFk6tbNyG+qSaIBZLyW9Xf3sWZHN/lxe9fTh1U45DpPf07yj94KsUHHWe4Yk1A==" crossorigin="anonymous"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-7s5uDGW3AHqw6xtJmNNtr+OBRJUlgkNJEo78P4b0yRw= sha512-nNo+yCHEyn0smMxSswnf/OnX6/KwJuZTlNZBjauKhTK0c+zT+q5JOCx0UFhXQ6rJR9jg6Es8gPuD2uZcYDLqSw==" crossorigin="anonymous">
    <link rel="stylesheet" href="CSS/main.css">
    <link rel="stylesheet" href="CSS/animate.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <title>Lor emIp sumDo lorSita</title>
    <link rel="stylesheet" href="CSS/lightbox.min.css">
    <script src="https://use.typekit.net/pzl7njn.js"></script>
    <link href="CSS/flexnav.css" rel="stylesheet" type="text/css" / >
</script>
    <script>
      try {
        Typekit.load({
          async: false
        });
      } catch (e) {}

    </script>
    <script src="JS/wow.min.js"></script>
              <script>
              new WOW().init();
              </script>
  <style id="jsbin-css">
/*----- Responsive Nav Start Credit - http://tinyurl.com/qepfqon -----*/

.clearfix:after {
  display: block;
  clear: both;
}


/*----- Menu Outline -----*/

.menu-wrap {
  width: 100%;
  background: #DAE6EB;
  margin: 0px auto
}

.menu {
  width: 1200px;
  margin: 0px;
  margin-bottom: -55px;
  opacity: 5;

}

.menu li {
  margin: 0px;
  list-style: none;
  font-family: 'industry';
  font-size: 18px;
}

.menu a {
  transition: all linear 0.15s;
  color: #98a1a4;
  font-size: 18px;
}

.menu li:hover > a,
.menu li:active > a,
.menu .current-item > a {
  text-decoration: none;
  color: #414546;
  font-size: 18px;
}

.menu .arrow {
  font-size: 11px;
  line-height: 0%;
}


/*----- Top Level -----*/

.menu > ul > li {
  float: left;
  display: inline-block;
  position: relative;
  font-size: 18px;
}

.menu > ul > li > a {
  padding: 10px 40px;
  display: inline-block;
}

.menu > ul > li:hover > a,
.menu > ul > li:active > a,
.menu > ul > .current-item > a {
  background: #98a1a4;
}


/*----- Bottom Level -----*/

.menu li:hover .sub-menu,
.menu li:active {
  z-index: 1;
  opacity: 1;
}

.sub-menu {
  width: 160%;
  padding: 5px 0px;
  position: absolute;
  top: 100%;
  left: 0px;
  z-index: -1;
  opacity: 0;
  transition: opacity linear 0.15s;
  background: #98a1a4B;
  text-align: left;
}

.sub-menu li {
  display: block;
  font-size: 18px;
}

.sub-menu li a {
  padding: 10px 10px;
  display: block;
  font-size: 18px;
}

.sub-menu li a:hover,
.sub-menu li a:active,
.sub-menu .current-item a {
  background: #98a1a4;
}

/*Jumbotron. Ignore*/
.jumbotron {
  background-image: url('https://images.unsplash.com/photo-1450849608880-6f787542c88a?crop=entropy&fit=crop&fm=jpg&h=1000&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925');
  background-repeat: no-repeat;
  background-position: center top;
  margin-top: 0px;
  margin-bottom: -0px;
}

.jumbotron .container {
  position: relative;
  left: auto;
  right: auto;
  height: 100vh;
  width: 100vw;
  padding: 100px 0;
  text-align: center;
}

.jumbotron h1 {
  color: #fff;
  font-size: 84px;
  font-family: "industry", sans-serif;
  font-style: normal;
  font-weight: 900;
  text-shadow: 3px 3px #000;
}

.jumbotron p {
  font-size: 24px;
  font-family: "industry", sans-serif;
  font-style: italic;
  font-weight: 1000;
  color: #f7f7f7;
}
</style>
</head> 


<section id="Top">
  </section>
    </div>
    <div class="menu-wrap">
      <nav class="menu">
        <ul class="clearfix" data-breakpoint="1200">
          <li class="current-item wow fadeInDown"><a href="#"><i class="fa fa-home fa"></i> Lore</a></li>
          <li class="wow fadeInDown delay05"><a href="#"><i class="fa fa-user fa"></i> mIpsu mD</a></li>
          <li class="wow fadeInDown delay05"><a href="#"><i class="fa fa-map fa"></i> olorS</a></li>
          <li class="wow fadeInDown delay15"><a href="#"><i class="fa fa-bolt fa"></i> itAmet.L</a></li>
          <li class="wow fadeInDown delay2"><a href="#"><i class="fa fa-paint-brush fa"></i> or emIps</a></li>
          <li class="wow fadeInDown delay25"><a href="#"><i class="fa fa-envelope fa"></i> umDolor</a></li>
          <li class="wow fadeInDown delay3"><a href="/blog/index.html"><i class="fa fa-pencil fa"></i> Sita</a></li>
        </ul>
      </nav>
    </div>
   <div class="jumbotron">
      <div class="container">
        <h1 class="wow fadeInLeft Big">Lor emIp sumDo lorSitA</h1>
        <p class="wow fadeInRight delay1">metLorem Ip sumDo lorSitAm.</p>
        <div class="container">
          <ul class="actions">
            <li class="wow fadeInUp Big delay2"><a href="#one" class="button special scrolly"><i class="fa fa-chevron-down fa"></i></a></li>
          </ul>
        </div>
      </div>
    </div>

I see that you are already using Bootstrap. Bootstrap makes this very easy for you to do as they have it built in already you just need to use their structure. Below I have put in their navbar structure into your existing code. I have taken out the menu css that you had and added just a couple of lines of css to get you started. You can mess with the css and change it to your liking but it should look almost identical to the setup that you had. In this code the nav will have a class of navbar-default which I have given a background of none and a position of absolute top to keep it at the top of the page and a width of 100%. Inside of it is a navbar-header and a navbar-collapse. In the navbar-header is your menu-hamburger which bootstrap has already styled and given javascript to open at a max-width of 767px. Then the navbar-collapse is where you have your links which I have given a media query style background of transparent black. So when you get to the 767 px it will be hidden until you click on the hamburger. This is all built into Bootstraps framework making it very easy for developers to get started on their sites. I hope this helps. Here is your revised code you should be able to just copy and paste this as is:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha256-KXn5puMvxCw+dAYznun+drMdG1IFl3agK0p/pqT9KAo= sha512-2e8qq0ETcfWRI4HJBzQiA3UoyFk6tbNyG+qSaIBZLyW9Xf3sWZHN/lxe9fTh1U45DpPf07yj94KsUHHWe4Yk1A==" crossorigin="anonymous"></script>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha256-7s5uDGW3AHqw6xtJmNNtr+OBRJUlgkNJEo78P4b0yRw= sha512-nNo+yCHEyn0smMxSswnf/OnX6/KwJuZTlNZBjauKhTK0c+zT+q5JOCx0UFhXQ6rJR9jg6Es8gPuD2uZcYDLqSw==" crossorigin="anonymous">
    <link rel="stylesheet" href="CSS/main.css">
    <link rel="stylesheet" href="CSS/animate.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
    <title>Lor emIp sumDo lorSita</title>
    <link rel="stylesheet" href="CSS/lightbox.min.css">
    <script src="https://use.typekit.net/pzl7njn.js"></script>
    <link href="CSS/flexnav.css" rel="stylesheet" type="text/css" / >
</script>
    <script>
      try {
        Typekit.load({
          async: false
        });
      } catch (e) {}

    </script>
    <script src="JS/wow.min.js"></script>
              <script>
              new WOW().init();
              </script>
  <style id="jsbin-css">
/*----- Responsive Nav Start Credit - http://tinyurl.com/qepfqon -----*/

.clearfix:after {
  display: block;
  clear: both;
}
.navbar-default {
  background:none;
  margin:0;
  position:absolute;
  top:0;
  left:0;
  width:100%;
  border:none;
  z-index:1;
}
.navbar-default .navbar-nav>li>a {
  font-size:18px;
}
.navbar-default .navbar-nav>li>a:hover,
.navbar-default .navbar-nav>li>a.active{
  background:#98a1a4;
  color:#333;
}
@media screen and (max-width: 767px){
  .navbar-collapse{background:rgba(0,0,0,0.8);}
}
/*Jumbotron. Ignore*/
.jumbotron {
  background-image: url('https://images.unsplash.com/photo-1450849608880-6f787542c88a?crop=entropy&fit=crop&fm=jpg&h=1000&ixjsv=2.1.0&ixlib=rb-0.3.5&q=80&w=1925');
  background-repeat: no-repeat;
  background-position: center top;
  margin-top: 0px;
  margin-bottom: -0px;
}

.jumbotron .container {
  position: relative;
  left: auto;
  right: auto;
  height: 100vh;
  width: 100vw;
  padding: 100px 0;
  text-align: center;
}

.jumbotron h1 {
  color: #fff;
  font-size: 84px;
  font-family: "industry", sans-serif;
  font-style: normal;
  font-weight: 900;
  text-shadow: 3px 3px #000;
}

.jumbotron p {
  font-size: 24px;
  font-family: "industry", sans-serif;
  font-style: italic;
  font-weight: 1000;
  color: #f7f7f7;
}
</style>
</head> 


<section id="Top">
  </section>
    <nav role="navigation" class="navbar navbar-default">
        <div class="navbar-header">
            <button type="button" data-target="#navbarCollapse" data-toggle="collapse" class="navbar-toggle">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <!-- Collection of nav links and other content for toggling -->
        <div id="navbarCollapse" class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li class="wow fadeInDown"><a class="active" href="#"><i class="fa fa-home fa"></i> Lore</a></li>
                <li class="wow fadeInDown delay05"><a href="#"><i class="fa fa-user fa"></i> mIpsu mD</a></li>
                <li class="wow fadeInDown delay05"><a href="#"><i class="fa fa-map fa"></i> olorS</a></li>
                <li class="wow fadeInDown delay15"><a href="#"><i class="fa fa-bolt fa"></i> itAmet.L</a></li>
                <li class="wow fadeInDown delay2"><a href="#"><i class="fa fa-paint-brush fa"></i> or emIps</a></li>
                <li class="wow fadeInDown delay25"><a href="#"><i class="fa fa-envelope fa"></i> umDolor</a></li>
                <li class="wow fadeInDown delay3"><a href="/blog/index.html"><i class="fa fa-pencil fa"></i> Sita</a></li>
            </ul>
        </div>
    </nav>
   <div class="jumbotron">
      <div class="container">
        <h1 class="wow fadeInLeft Big">Lor emIp sumDo lorSitA</h1>
        <p class="wow fadeInRight delay1">metLorem Ip sumDo lorSitAm.</p>
        <div class="container">
          <ul class="actions">
            <li class="wow fadeInUp Big delay2"><a href="#one" class="button special scrolly"><i class="fa fa-chevron-down fa"></i></a></li>
          </ul>
        </div>
      </div>
    </div>
  </body>
</html>

I'm not sure anyone is going to write the code for you, but we can point you in the right direction. What you are looking for, in terms of specifying different styles at different screen sizes are called media queries. They look like this:

@media screen and (max-width: 1199px) {...}

I have built something very similar to what you are looking for (although mine has probably more than what you are looking for) so if you need a reference on how things work or whatnot, I'll post a link.

http://codepen.io/mhodges44/pen/dGMMOK

You can ignore the angularJS stuff (all of the ng-* in the HTML and the javascript), just focus on the CSS media queries. That should give you a pretty good idea of how it all works and you should be able to apply it to your situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM