javascript - Improve this Jquery toggle menu (responsive) -
i want improve jquery toggle menu (responsive).
first part:
i want when resize window under 768px
, click hamburger toggle menu (hamburger icon changes x icon) full "nav ul" menu turn visible in block style , still visible when resize window until break-point 768px
, disappears.
second part:
when resize down 768px
, under, "nav ul"
must visible in block style , toggle menu icon must "x"
(not hamburger).
(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: 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 .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>
using twitter bootstrap , responsive classes, can done. there built-in dropdown in addition many classes showing/hiding elements on various screen sizes.
Comments
Post a Comment