Two navigation classes for responsive design?

I've been learning the basics of web design for a month now, and I'm trying to create a semantically correct responsive header for a fictitious website. The code I wrote today creates the look that I am aiming for, but the problem is that I'm not sure if the code is really good, and if I'm on the right track.

// Sidenav Toggle //
function togNav() {
  "use strict";
  var side = document.getElementById("sidenav"),
    main = document.getElementById("container");

  if (side.style.transform === "translateX(0%)") {
    side.style.transform = "translateX(-100%)";
    main.style.transform = "translateX(0%)";
  } else {
    side.style.transform = "translateX(0%)";
    main.style.transform = "translateX(40%)";
  }
}
/* Side Navigation */

#sidenav {
  /* Styling */
  width: 40%;
  height: 100%;
  background: #fff;
  box-shadow: 0 0 1rem rgba(0, 0, 0, 0.3);
  /* Positioning */
  top: 0;
  z-index: 2;
  position: fixed;
  /* UX */
  transition: 0.3s;
  transform: translateX(-100%);
}

#sidenav a {
  padding: 3rem;
  display: block;
}

#sidenav a:hover {
  color: #8cc63e;
  transition: 0s;
}


/* /////////////////////////////////////////////////// */
/* ////////////////////// HEADER ///////////////////// */
/* /////////////////////////////////////////////////// */


/* Header - Container */

header {
  /* Styling */
  height: 50rem;
  background: lightblue;
}


/* Header - Small Navigation */

header>nav.small {
  /* Styling */
  background: #fff;
  box-shadow: 0 0 1rem rgba(0, 0, 0, 0.3);
}

header>nav.small>.wrapper {
  /* Styling */
  height: 8rem;
  padding: 0 3rem;
  /* Flexbox */
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header>nav.small #burger {
  cursor: pointer;
  transition: 0.3s;
}

header>nav.small #burger .bar {
  /* Style */
  width: 2.25rem;
  height: 0.3rem;
  background: #000;
  margin-top: 0.4rem;
}


/* Header - Large Navigation */

header>nav.large {
  /* Styling */
  background: #fff;
  box-shadow: 0 0 1rem rgba(0, 0, 0, 0.3);
}

header>nav.large>.wrapper {
  /* Styling */
  height: 8rem;
  margin: auto;
  padding: 0 3rem;
  max-width: 96rem;
  /* Flexbox */
  display: flex;
  align-items: center;
  justify-content: space-between;
}

header>nav.large ul li {
  display: inline-block;
}

header>nav.large ul li a {
  padding-left: 4.5rem;
}

header>nav.large ul li a:hover {
  color: #8cc63e;
}


/* Header - Media Queries */

@media only screen and (min-width: 600px) {
  header>nav.small {
    display: none;
  }
}

@media only screen and (max-width: 600px) {
  header>nav.large {
    display: none;
  }
}


/* General Styling */

html {
  font-size: 62.5%;
}

body,
a {
  margin: 0;
  color: #333;
  font-size: 1.6rem;
  font-family: "Source Sans Pro";
  text-decoration: none;
}

#container {
  transition: 0.3s;
}
<!-- Off Canvas -->
<div id="sidenav">
  <a href="#">Link 1</a>
  <a href="#">Link 2</a>
  <a href="#">Link 3</a>
  <a href="#">Link 4</a>
</div>

<!-- On Canvas -->
<div id="container">
  <header>
    <nav class="small">
      <div class="wrapper">
        <div id="burger" onclick="togNav()">
          <div class="bar bar1"></div>
          <div class="bar bar2"></div>
          <div class="bar bar3"></div>
        </div>
        <a href="#" class="logo">
                        Logo
                    </a>
      </div>
    </nav>
    <nav class="large">
      <div class="wrapper">
        <a href="#" class="logo">
                        Logo
                    </a>
        <ul>
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
          <li><a href="#">Link 3</a></li>
          <li><a href="#">Link 4</a></li>
        </ul>
      </div>
    </nav>
  </header>
</div>
Run codeHide result

View in CodePen

As you can see, I used two navigation classes to create an integrated navigation panel for large screens and a top panel with a hidden side panel for small screens. I use Flexbox to align elements and Javascript to display side navigation.


: ? , , , ? .

, . Google StackOverflow , , , , . , - , , ! , .

+4
1
  • , .
  • , , , ( ).
  • ( - ): " , " . , -, @media. , ( , , DRY, , / , , , - , , ).

(, -, ), , - : , , 40% . . , , . ( 250px 100vw).

function togNav() {
  document.querySelector("header").classList.toggle('opened');
}
#burger {
  position: absolute;
  left: calc(100% + 12px);
  top: 32px;
  width: 45px;
  height: 32px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  cursor: pointer;
  padding: 12px;
}

#burger>div {
  width: 45px;
  height: 6px;
  background: #000;
}

header {
  background: #fff;
  box-shadow: 0 3px 5px -1px rgba(0, 0, 0, 0.1), 0 6px 10px 0 rgba(0, 0, 0, 0.07), 0 1px 18px 0 rgba(0, 0, 0, 0.06);
  height: 8rem;
  padding: 0 3rem;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#menu {
  display: flex;
  background-color: #fff;
  padding: 0;
  margin: 0;
}

#menu li {
  display: block;
  border: 0 solid #eee;
  border-right-width: 1px;
}

#menu li:last-child {
  border-right-width: 0;
}

#menu a {
  display: block;
  padding: 1rem;
  color: black;
  text-decoration: none;
}

#menu a:hover {
  color: #8cc63e;
}

body {
  margin: 0;
  overflow: hidden;
  background-color: lightblue;
}

nav {
  top: 0;
  left: 0;
  background-color: white;
}

@media (max-width: 600px) {
  nav {
    height: 100%;
    position: fixed;
    width: 250px;
    max-width: calc(100vw - 100px);
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.3s linear;
  }
  .logo {
    transform: translateX(0);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }
  .opened .logo {
    transform: translateX(65vw);
  }
  header {
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    transition: box-shadow .3s linear;
  }
  header.opened {
    box-shadow: none;
  }
  header.opened nav {
    transform: translateX(0);
    box-shadow: 0 3px 5px -1px rgba(0, 0, 0, 0.2), 0 6px 10px 0 rgba(0, 0, 0, 0.14), 0 1px 18px 0 rgba(0, 0, 0, 0.12);
  }
  #menu {
    flex-direction: column;
    padding: 0;
    margin: 0;
  }
  #menu li {
    border-bottom: 1px solid #eee;
  }
}
<header>
  <a href class="logo">Logo</a>
  <nav>
    <div id="burger" onclick="togNav()">
      <div></div>
      <div></div>
      <div></div>
    </div>
    <ul id="menu">
      <li><a href>Link 1</a></li>
      <li><a href>Link 2</a></li>
      <li><a href>Link 3</a></li>
      <li><a href>Link 4</a></li>
    </ul>
  </nav>
</header>
Hide result

:

  • JavaScript

SCSS, .


: , , , , , , , 5 . , , 2 , ( ) / SO. : , , , , .

+1

Source: https://habr.com/ru/post/1693889/


All Articles