[WIP] Responsive Mega Menus

In this example below you will see how to do a [WIP] Responsive Mega Menus with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by praliedutzel, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright praliedutzel ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>[WIP] Responsive Mega Menus</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <header class="header">
  <a href="#" class="header__logo">
    Company
  </a>
  <nav class="main-menu">
    <ul>
      <li class="has-mega-menu">
        <a href="#">Link 1</a>
        <div class="mega-menu">
          <div class="mega-menu__section">
            <a href="#" class="mega-menu__section-title">Section 1</a>
            <ul>
              <li><a href="#">Section 1 Link 1</a></li>
              <li><a href="#">Section 1 Link 2</a></li>
              <li><a href="#">Section 1 Link 3</a></li>
            </ul>
          </div>
          <div class="mega-menu__section">
            <a href="#" class="mega-menu__section-title">Section 2</a>
            <ul>
              <li><a href="#">Section 2 Link 1</a></li>
              <li><a href="#">Section 2 Link 2</a></li>
              <li><a href="#">Section 2 Link 3</a></li>
            </ul>
          </div>
        </div>
      </li>
      <li><a href="#">Link 2</a></li>
      <li><a href="#">Link 3</a></li>
    </ul>
  </nav>
</header>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/praliedutzel/wip-responsive-mega-menus-xZVjMg */
body {
  font-family: Roboto, sans-serif;
  font-size: 14px;
}

.header {
  background-color: #232429;
  height: 85px;
  color: #fff;
}
.header:after {
  content: "";
  display: table;
  clear: both;
}
.header a {
  color: #fff;
  text-decoration: none;
}

.header__logo {
  padding: 2rem 1rem;
  display: inline-block;
}

.main-menu {
  float: right;
}

.main-menu ul {
  margin: 0;
  padding-left: 0;
  list-style: none;
}

.main-menu > ul > li {
  display: inline-block;
}

.main-menu > ul > li > a {
  padding: 2rem 1rem;
  line-height: 85px;
}

.main-menu .has-mega-menu {
  background-color: transparent;
  transition: background-color 0.3s ease-in-out;
}
.main-menu .has-mega-menu > a:after {
  content: "\25BC";
  color: #9ea1ad;
  font-size: 8px;
}
.main-menu .has-mega-menu.mega-menu-is-open {
  background-color: #464852;
}

.mega-menu {
  background-color: #464852;
  top: 85px;
  right: 0;
  padding: 1rem;
  position: absolute;
  visibility: hidden;
  opacity: 0;
  transition: visibility 0.3s ease-in-out, opacity 0.3s ease-in-out;
}
.mega-menu.is-open {
  visibility: visible;
  opacity: 1;
}

.mega-menu__section {
  padding: 1rem;
  display: inline-block;
}

.mega-menu__section-title {
  margin-bottom: 0.5rem;
  padding-bottom: 0.25rem;
  display: block;
  border-bottom: 1px solid #9ea1ad;
}


/*Downloaded from https://www.codeseek.co/praliedutzel/wip-responsive-mega-menus-xZVjMg */
if ($(window).width() < 1024) {
  mobile = true;
} else {
  mobile = false;
}

(function ($, sr) {
    // debouncing function from John Hann
    // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
    var debounce = function (func, threshold, execAsap) {
        var timeout;

        return function debounced() {
            var obj = this, args = arguments;
            function delayed() {
                if (!execAsap)
                    func.apply(obj, args);
                timeout = null;
            };

            if (timeout)
                clearTimeout(timeout);
            else if (execAsap)
                func.apply(obj, args);

            timeout = setTimeout(delayed, threshold || 100);
        };
    }
    // smartresize 
    jQuery.fn[sr] = function (fn) { return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };

})(jQuery, 'smartresize');
$(window).smartresize(function () {
    if ($(window).width() < 1024) {
        mobile = true;
    }
    else {
        mobile = false;
    }
});

if (mobile != true) {
  $('.has-mega-menu').hover(
    function() {
      $(this).addClass('mega-menu-is-open');
      $('.mega-menu', this).addClass('is-open');
    },
    function() {
      $(this).removeClass('mega-menu-is-open');
      $('.mega-menu', this).removeClass('is-open');
    }
  );
}

$('.has-mega-menu > a').not('.binded').addClass('binded').on('click', function() {
  if (mobile == true) {
    $(this).next('.mega-menu').slideToggle();
    return false;
  }
});

Comments