改进此Jquery切换菜单(响应)

时间:2016-07-19 12:54:32

标签: javascript jquery html css responsive

我想改进我的jQuery切换菜单(响应)。

第一部分:

我希望在768px下调整窗口大小并点击汉堡切换菜单(汉堡包图标更改为X图标)时,完整的“导航ul”菜单会以块样式显示,并且在我调整大小时仍然可见窗口直到断点768px,然后消失。

第二部分:

当我向下调整回768px及以下时,"nav ul"必须以块样式显示,并且切换菜单图标必须是"X"(不是汉堡包)。

(function($, window, document, undefined) {
  'use strict';

  $(function() {

    $(".menu_bar").click(function() {
      $(this).toggleClass("active");
      $("#mobileMenu").slideToggle(100);
    });
  });
  $(window).on("resize", function() {
    if ($(this).width() > 768) {
      $("#mobileMenu").show();
      $(".menu_bar").removeClass("active");

    } else {
      $(".menu_bar").removeClass("active");
      $("#mobileMenu").hide();
    }
  });
})(jQuery, window, document);
.container{
 padding:10px;
  }
.menu_bar{
display: none;
}
#header{
    background: #fff;
}
.header{
display:flex;
justify-content: space-between;
align-items: center;
}
.header .logo{
font-family: 'Ubuntu', sans-serif;
padding: 20px 0;
max-width: 288px;
}
nav ul li{
float: left;
}

nav ul li a{
font-family: 'Ubuntu', sans-serif;
font-weight: lighter;
font-size: 1.2em;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #585858;
display: block;
padding: 10px 14px;
transition: all 0.3s ease;
}
nav ul li{
margin-left: 4px;
}
nav ul li a.btn_active{
margin-left: 0px;
}
nav ul li a:hover{
font-family: 'Ubuntu', sans-serif;
background: #028a9b;
font-weight: lighter;
color: #fff;
border-radius: 2px;
}
nav ul li a .active:hover{
font-family: 'Ubuntu', sans-serif;
background: #028a9b;
font-weight: lighter;
color: #fff;
border-radius: 2px;
}
nav ul li span{
margin: 10px 2px 0 2px;
float:left;
width:1px;
height:24px;
background: #c1c1c1;
display: block;
content: '';
}

@media (max-width: 768px){
    .header img {
    width: 75%;
    margin-top: 6px;
    }
    .menu_bar{
    position: fixed;
    top: 30px;
    right: 10px;
    display: block;
    width: 40px;
    height: 40px;
    cursor: pointer;
    z-index:1000;
    }
    .menu_bar span.hamburguer{
    position: absolute;
    display: block;
    width: 28px;
    height: 1px;
    top: 12px;
    left: 0;
    background: #028a9b;
    -webkit-transition: background 0s 0.3s;
    transition: background 0s 0.3s
    }
    .menu_bar span.hamburguer::before{
    position: absolute;
    display: block;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #028a9b;
    content: "";
    top: -8px;
    -webkit-transition-duration: 0.3s, 0.3s;
   transition-duration: 0.3s, 0.3s;
   -webkit-transition-delay: 0.3s, 0s;
   transition-delay: 0.3s, 0s;
   -webkit-transition-property: top,
   -webkit-transform;
   transition-property: top, transform;
    }
    .menu_bar span.hamburguer::after{
    position: absolute;
    display: block;
    left: 0;
    width: 100%;
    height: 1px;
    background-color: #028a9b;
    content: "";
    top: 8px;
    -webkit-transition-duration: 0.3s, 0.3s;
    transition-duration: 0.3s, 0.3s;
    -webkit-transition-delay: 0.3s, 0s;
    transition-delay: 0.3s, 0s;
    -webkit-transition-property: bottom,
    -webkit-transform;
    transition-property: bottom, transform;
    }
    .menu_bar.active span.hamburguer{
    background: none;
    }
    .menu_bar.active span.hamburguer::before{
    background: #028a9b;
    top:0;
    -webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
    -webkit-transition-delay: 0s, 0.3s;
    transition-delay: 0s, 0.3s;
    }
    .menu_bar.active span.hamburguer::after{
    background: #028a9b;
    top: 0;
    -webkit-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    transform: rotate(-45deg);
    -webkit-transition-delay: 0s, 0.3s;
    transition-delay: 0s, 0.3s;
    }

    #mobileMenu{
    display:none;
    }
    nav{
    position: absolute;
    z-index: 20000;
    top: 86px;
    width: 100%;
    left: 0%;

    }
    nav ul{
    width: 100%;
    background: #ececec;
    height: 100%;
    padding:0 0 100% 0;
    height: 100%;
    opacity: 0.95;
    }
    .btn_active{
    border:none;
    }
    nav ul li{
    display: block;
    width: 100%;
    text-align: center;
     border-bottom: 1px solid #c3c3c3;
     padding:0;
    }
    nav ul li:nth-child(1){
     border-bottom: 1px solid #c3c3c3;
     border-top: 1px solid #c3c3c3;
    }
    nav ul li a{
    padding: 22px 14px;
    }
    nav ul li a:hover{
    height: 100%;
    }

    #header{
    background: white;
    position: fixed;
    width: 100%;
    top: 0;
    z-index: 99;
    }

  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<header>
      <section id="header" class="container-fluid">
        <div class="container">
          <div class="header">

            <div class="logo">LOGO</div>

            <div class="menu_bar ">
              <span class="hamburguer">
              </span>
            </div>

            <nav id="mobileMenu">
              <ul>
                <li><a>One</a></li>
                <li><a>Two</a></li>
                <li><a>Three</a></li>
                <li><a>Four</a></li>
              </ul>
            </nav>

          </div>
        </div>
      </section>
 </header>

1 个答案:

答案 0 :(得分:-1)

使用Twitter Bootstrap及其响应类,可以轻松完成。除了用于在各种屏幕尺寸上显示/隐藏元素的许多类之外,还有内置下拉列表。