如何修复此菜单以便它可以响应?

时间:2016-07-16 14:09:29

标签: javascript jquery html css

我有这个菜单:(片段很长),请全屏查看片段...



$(document).ready(function() {
  $('.burger').click(function() {
    $(this).toggleClass('open');
    $('.header_menu').toggleClass('is_open');
    $('span').toggleClass('close');
    $('#menu').toggleClass('menu_open');
  });
});

@import url(https://fonts.googleapis.com/css?family=Raleway:400,200,300,800,900,800italic,700);
@import url(https://fonts.googleapis.com/css?family=Lobster);
@import url(https://fonts.googleapis.com/css?family=Montserrat:400,700);
::selection {
  background: transparent;
  color: #999;
}
body {
  margin: 0;
  padding: 0;
}
::-scrollbar {
  display: none;
}
* {
  box-sizing: border-box;
}
#header_wrapper {
  width: 100%;
  position: fixed;
}
#header {
  width: 80%;
  margin: 1rem auto;
  position: relative;
  display: flex;
  justify-content: space-between;
}
.header_logo {
  margin: 0;
  left: 0;
  display: inline-block;
  font-family: "Raleway";
  font-weight: bold;
  color: #ffffff;
  background-color: #111;
  padding: .2em;
  font-size: 40px;
}
.header_logo::-moz-selection {
  background: transparent;
}
.header_logo a {
  text-decoration: none;
  color: white;
}
.header_logo::-o-selection {
  background: transparent;
}
.header_logo::-webkit-selection {
  background: transparent;
}
.header_logo::selection {
  background: transparent;
}
.burger {
  border: 0;
  position: absolute;
  right: 0;
  background: none;
  outline: 0;
  padding: 0;
  cursor: pointer;
  border-bottom: 5px solid currentColor;
  width: 40px;
  top: 39%;
  transform: translateX(-50%);
}
.burger::-moz-focus-inner {
  border: 0;
  padding: 0;
}
.burger:before {
  content: "";
  display: block;
  border-bottom: 5px solid currentColor;
  width: 100%;
  margin-bottom: 5px;
  transition: 0ms;
  transition-delay: 1500ms;
}
.burger:after {
  content: "";
  display: block;
  margin-bottom: 5px;
  border-bottom: 5px solid currentColor;
  width: 100%;
  transition: 0ms;
  transition-delay: 1500ms;
}
.burger.open {
  border-bottom: none;
  transition: border-bottom 0.8s ease-in-out;
  -webkit-transition: border-bottom 0.8s ease-in-out;
}
.burger.open:before {
  border-bottom: 5px solid #fff;
  transform: rotate(-405deg) translateY(1px) translateX(-3px);
  -webkit-transform: rotate(-405deg) translateY(1px) translateX(-3px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}
.burger.open:after {
  border-bottom: 5px solid #fff;
  transform: rotate(405deg) translateY(-4px) translateX(-5px);
  -webkit-transform: rotate(405deg) translateY(-4px) translateX(-5px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}
.header_menu {
  width: 90%;
  position: absolute;
  background: #000;
  top: 0;
  right: 0;
  left: 100%;
  height: 100%;
  pointer-events: none;
  transition: transform 300ms ease-in-out;
  transition-delay: 1200ms;
}
.header_menu.is_open {
  transform: translateX(-100%);
  pointer-events: auto;
  transition: transform 300ms ease-in-out;
  transition-delay: 400ms;
}
span {
  position: absolute;
  top: 50%;
  left: 2%;
  display: block;
  color: #fff;
  font-family: "Raleway";
  font-weight: bold;
  transform: translateY(-50%);
  transition: all 300ms ease-in;
  transition-delay: 1400ms;
}
span.close {
  opacity: 0;
  transition: all 300ms ease-in;
}
#menu {
  width: 100%;
  text-align: center;
  padding: 0;
  margin: 0;
  position: absolute;
  top: 50%;
  left: 100%;
  transform: translateY(-50%);
  text-decoration: none;
  transition: transform 1000ms ease-in-out;
  transition-delay: 400ms;
}
#menu.menu_open {
  transform: translateX(-100%) translateY(-50%);
  transition: transform 400ms ease-in-out;
  transition-delay: 600ms;
  background-color: transparent;
}
#menu li {
  list-style: none;
  display: inline-block;
  margin-left: 1em;
  z-index: 99999;
}
#menu li a {
  text-decoration: none;
  color: white;
  font-family: "Raleway";
  text-transform: uppercase;
  font-weight: bold;
  z-index: 99999;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header_wrapper">
  <div id="header">
    <div class="header_logo">
      <a href="/about_me">JD</a> 
    </div>

    <div class="header_menu">
      <span>MENU</span> 
      <ul id="menu">
        <li><a href="/home">Home</a>
        </li>
        <li><a href="#">About me</a>
        </li>
        <li><a href="#">Work</a>
        </li>
        <li><a href="#">Tools</a>
        </li>
        <li><a href="#">Blog</a>
        </li>
        <li><a href="#">Contact</a>
        </li>
        <li><a href="#">Services</a>
        </li>
      </ul>
    </div>

    <div class="burger">
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

它看起来很不错,但问题是屏幕较小,它覆盖了徽标和汉堡,我对小屏幕的想法是这样的:

enter image description here

但我不知道如何实现这一点,也许我在代码中有一些错误,或者我的概念是错误的...... 请帮忙。

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$(document).ready(function() {
  $('.burger').click(function() {
    $(this).toggleClass('open');
    $('.header_menu').toggleClass('is_open');
    $('.header_menu').toggleClass('hidden');
    $('span').toggleClass('close');
    $('#menu').toggleClass('menu_open');
  });
});
&#13;
@import url(https://fonts.googleapis.com/css?family=Raleway:400,200,300,800,900,800italic,700);
@import url(https://fonts.googleapis.com/css?family=Lobster);
@import url(https://fonts.googleapis.com/css?family=Montserrat:400,700);
::selection {
  background: transparent;
  color: #999;
}
body {
  margin: 0;
  padding: 0;
}
::-scrollbar {
  display: none;
}
* {
  box-sizing: border-box;
}
#header_wrapper {
  width: 100%;
  position: fixed;
}
#header {
  width: 80%;
  margin: 1rem auto;
  position: relative;
  display: flex;
  justify-content: space-between;
}
.header_logo {
  margin: 0;
  left: 0;
  display: inline-block;
  font-family: "Raleway";
  font-weight: bold;
  color: #ffffff;
  background-color: #111;
  padding: .2em;
  font-size: 40px;
}
.header_logo::-moz-selection {
  background: transparent;
}
.header_logo a {
  text-decoration: none;
  color: white;
}
.header_logo::-o-selection {
  background: transparent;
}
.header_logo::-webkit-selection {
  background: transparent;
}
.header_logo::selection {
  background: transparent;
}
.burger {
  border: 0;
  position: absolute;
  right: 0;
  background: none;
  outline: 0;
  padding: 0;
  cursor: pointer;
  border-bottom: 5px solid currentColor;
  width: 40px;
  top: 39%;
  transform: translateX(-50%);
}
.burger::-moz-focus-inner {
  border: 0;
  padding: 0;
}
.burger:before {
  content: "";
  display: block;
  border-bottom: 5px solid currentColor;
  width: 100%;
  margin-bottom: 5px;
  transition: 0ms;
  transition-delay: 1500ms;
}
.burger:after {
  content: "";
  display: block;
  margin-bottom: 5px;
  border-bottom: 5px solid currentColor;
  width: 100%;
  transition: 0ms;
  transition-delay: 1500ms;
}
.burger.open {
  border-bottom: none;
  transition: border-bottom 0.8s ease-in-out;
  -webkit-transition: border-bottom 0.8s ease-in-out;
}
.burger.open:before {
  border-bottom: 5px solid #fff;
  transform: rotate(-405deg) translateY(1px) translateX(-3px);
  -webkit-transform: rotate(-405deg) translateY(1px) translateX(-3px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}
.burger.open:after {
  border-bottom: 5px solid #fff;
  transform: rotate(405deg) translateY(-4px) translateX(-5px);
  -webkit-transform: rotate(405deg) translateY(-4px) translateX(-5px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}
.header_menu {
  width: 90%;
  position: absolute;
  background: #000;
  top: 0;
  right: 0;
  left: 100%;
  height: 100%;
  pointer-events: none;
  transition: transform 300ms ease-in-out;
  transition-delay: 1200ms;
}
.header_menu.is_open {
  transform: translateX(-100%);
  pointer-events: auto;
  transition: transform 300ms ease-in-out;
  transition-delay: 400ms;
}
span {
  position: absolute;
  top: 50%;
  left: 2%;
  display: block;
  color: #fff;
  font-family: "Raleway";
  font-weight: bold;
  transform: translateY(-50%);
  transition: all 300ms ease-in;
  transition-delay: 1400ms;
}
span.close {
  opacity: 0;
  transition: all 300ms ease-in;
}
#menu {
  width: 100%;
  text-align: center;
  padding: 0;
  margin: 0;
  position: absolute;
  top: 50%;
  left: 100%;
  transform: translateY(-50%);
  text-decoration: none;
  transition: transform 1000ms ease-in-out;
  transition-delay: 400ms;
}
#menu.menu_open {
  transform: translateX(-100%) translateY(-50%);
  transition: transform 400ms ease-in-out;
  transition-delay: 600ms;
  background-color: transparent;
}
#menu li {
  list-style: none;
  display: inline-block;
  margin-left: 1em;
  z-index: 99999;
}
#menu li a {
  text-decoration: none;
  color: white;
  font-family: "Raleway";
  text-transform: uppercase;
  font-weight: bold;
  z-index: 99999;
}
@media (max-width: 700px) {
 .hidden {
  display: none;
 }
 .header_menu {
   min-height: 200px;    
   width: 100%;
   top: 64px;
 }
 .burger {
   background-color: #000;
 }
 #menu li {
   display: block;
 }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header_wrapper">
  <div id="header">
    <div class="header_logo">
      <a href="/about_me">JD</a> 
    </div>

    <div class="header_menu hidden">
      <span>MENU</span> 
      <ul id="menu">
        <li><a href="/home">Home</a>
        </li>
        <li><a href="#">About me</a>
        </li>
        <li><a href="#">Work</a>
        </li>
        <li><a href="#">Tools</a>
        </li>
        <li><a href="#">Blog</a>
        </li>
        <li><a href="#">Contact</a>
        </li>
        <li><a href="#">Services</a>
        </li>
      </ul>
    </div>

    <div class="burger">
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用bootstrap库实现此目的。尝试这样,希望它会有所帮助。

$(document).ready(function() {
  $('.burger').click(function() {
$(this).toggleClass('open');
$('.header_menu').toggleClass('is_open');
$('.header_menu_sm').toggleClass('is_open');
$('span').toggleClass('close');
$('#menu').toggleClass('menu_open');
  });
});
@import url(https://fonts.googleapis.com/css?family=Raleway:400,200,300,800,900,800italic,700);
@import url(https://fonts.googleapis.com/css?family=Lobster);
@import url(https://fonts.googleapis.com/css?family=Montserrat:400,700);
::selection {
  background: transparent;
  color: #999;
}
body {
  margin: 0;
  padding: 0;
}
::-scrollbar {
  display: none;
}
* {
  box-sizing: border-box;
}
#header_wrapper {
  width: 100%;
  position: fixed;
}
#header {
  width: 80%;
  margin: 1rem auto;
  position: relative;
  display: flex;
  justify-content: space-between;
}
.header_logo {
  margin: 0;
  left: 0;
  display: inline-block;
  font-family: "Raleway";
  font-weight: bold;
  color: #ffffff;
  background-color: #111;
  padding: .2em;
  font-size: 40px;
}
.header_logo::-moz-selection {
  background: transparent;
}
.header_logo a {
  text-decoration: none;
  color: white;
}
.header_logo::-o-selection {
  background: transparent;
}
.header_logo::-webkit-selection {
  background: transparent;
}
.header_logo::selection {
  background: transparent;
}
.burger {
  border: 0;
  position: absolute;
  right: 0;
  background: none;
  outline: 0;
  padding: 0;
  cursor: pointer;
  border-bottom: 5px solid currentColor;
  width: 40px;
  top: 39%;
  transform: translateX(-50%);
}
.burger::-moz-focus-inner {
  border: 0;
  padding: 0;
}
.burger:before {
  content: "";
  display: block;
  border-bottom: 5px solid currentColor;
  width: 100%;
  margin-bottom: 5px;
  transition: 0ms;
  transition-delay: 1500ms;
}
.burger:after {
  content: "";
  display: block;
  margin-bottom: 5px;
  border-bottom: 5px solid currentColor;
  width: 100%;
  transition: 0ms;
  transition-delay: 1500ms;
}
.burger.open {
  border-bottom: none;
  transition: border-bottom 0.8s ease-in-out;
  -webkit-transition: border-bottom 0.8s ease-in-out;
}
.burger.open:before {
  border-bottom: 5px solid #fff;
  transform: rotate(-405deg) translateY(1px) translateX(-3px);
  -webkit-transform: rotate(-405deg) translateY(1px) translateX(-3px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}
.burger.open:after {
  border-bottom: 5px solid #fff;
  transform: rotate(405deg) translateY(-4px) translateX(-5px);
  -webkit-transform: rotate(405deg) translateY(-4px) translateX(-5px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}

.burger.hidden-lg.open {
  border-bottom: none;
  transition: border-bottom 0.8s ease-in-out;
  -webkit-transition: border-bottom 0.8s ease-in-out;
}
.burger.hidden-lg.open:before {
  border-bottom: 5px solid #000;
  transform: rotate(-405deg) translateY(1px) translateX(-3px);
  -webkit-transform: rotate(-405deg) translateY(1px) translateX(-3px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}
.burger.hidden-lg.open:after {
  border-bottom: 5px solid #000;
  transform: rotate(405deg) translateY(-4px) translateX(-5px);
  -webkit-transform: rotate(405deg) translateY(-4px) translateX(-5px);
  transition: transform 0.5s ease-in-out;
  transform-origin: center;
  -webkit-transition: -webkit-transform 0.5s ease-in-out;
}

.header_menu {
  width: 100%;
  position: absolute;
  background: #000;
  top: 0;
  right: 0;
  left: 100%;
  height: 100%;
  pointer-events: none;
  transition: transform 300ms ease-in-out;
  transition-delay: 1200ms;
}
.header_menu.is_open {
  transform: translate(-90%);
  pointer-events: auto;
  transition: transform 300ms ease-in-out;
  transition-delay: 400ms;
}
.header_menu_sm {
  width: 90%;
  position: absolute;
  background: #000;
  margin-top: 10%;
  right: 0;
  left: 100%;
  height: auto;
  pointer-events: none;
  transition: transform 300ms ease-in-out;
  transition-delay: 1200ms;
}
.header_menu_sm.is_open {
  transform: translate(-100%,15%);
  pointer-events: auto;
  transition: transform 300ms ease-in-out;
  transition-delay: 400ms;
}
span {
  position: absolute;
  top: 50%;
  left: 2%;
  display: block;
  color: #fff;
  font-family: "Raleway";
  font-weight: bold;
  transform: translateY(-50%);
  transition: all 300ms ease-in;
  transition-delay: 1400ms;
}
span.close {
  opacity: 0;
  transition: all 300ms ease-in;
}
#menu {
  width: 100%;
  text-align: center;
  padding: 0;
  margin: 0;
  position: absolute;
  top: 50%;
  left: 100%;
  transform: translateY(-50%);
  text-decoration: none;
  transition: transform 1000ms ease-in-out;
  transition-delay: 400ms;
}
#menu.menu_open {
  transform: translateX(-100%) translateY(-50%);
  transition: transform 400ms ease-in-out;
  transition-delay: 600ms;
  background-color: transparent;
}
#menu li {
  list-style: none;
  display: inline-block;
  margin-left: 1em;
  z-index: 99999;
}
#menu_sm {
  width: 100%;
  text-align: center;
  padding: 0;
  margin: 0;
  top: 50%;
  left: 100%;
  transform: translateY(0%);
  text-decoration: none;
  transition: transform 1000ms ease-in-out;
  transition-delay: 400ms;
}
#menu_sm.menu_open {
  transform: translateX(-100%) translateY(-50%);
  transition: transform 400ms ease-in-out;
  transition-delay: 600ms;
  background-color: transparent;
}
#menu_sm li {
  list-style: none;
  display: block;
  margin-left: 1em;
  z-index: 99999;
}
#menu li a {
  text-decoration: none;
  color: white;
  font-family: "Raleway";
  text-transform: uppercase;
  font-weight: bold;
  z-index: 99999;
}
#menu_sm li a {
  text-decoration: none;
  color: white;
  font-family: "Raleway";
  text-transform: uppercase;
  font-weight: bold;
  z-index: 99999;
}
<!DOCTYPE html>
<html>

  <head>
<link rel="stylesheet" href="style.css">
<script   src="https://code.jquery.com/jquery-2.2.4.min.js"   integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="   crossorigin="anonymous"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
   
<script src="script.js"></script>
  </head>

  <body>
<div id="header_wrapper">
  <div id="header">
    <div class="header_logo ">
      <a href="/about_me">JD</a> 
    </div>

    <div class="header_menu hidden-md hidden-xs hidden-sm">
      <span>MENU</span> 
      <ul id="menu">
        <li><a href="/home">Home</a>
        </li>
        <li><a href="#">About me</a>
        </li>
        <li><a href="#">Work</a>
        </li>
        <li><a href="#">Tools</a>
        </li>
        <li><a href="#">Blog</a>
        </li>
        <li><a href="#">Contact</a>
        </li>
        <li><a href="#">Services</a>
        </li>
      </ul>
      
    </div>
    
  
    <div class="burger hidden-md hidden-xs hidden-sm">
    </div>
    
    <div class="burger hidden-lg hidden-xl">
    </div>

    
  </div>
</div>

<div class="header_menu_sm hidden-lg hidden-xl">
      <span>MENU</span> 
      <ul id="menu_sm">
        <li><a href="/home">Home</a>
        </li>
        <li><a href="#">About me</a>
        </li>
        <li><a href="#">Work</a>
        </li>
        <li><a href="#">Tools</a>
        </li>
        <li><a href="#">Blog</a>
        </li>
        <li><a href="#">Contact</a>
        </li>
        <li><a href="#">Services</a>
        </li>
      </ul>
    </div>
    
  </body>

</html>