Show Menu On Click JavaScript

Show Menu On Click JavaScript
Code Snippet:hidden menu
Author: Denis
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 791
License: MIT
Edit Code online: View on CodePen
Read More

This simple JavaScript code snippet helps you to create a responsive menu that you can show on button click events. The core functionality is toggling the menu’s visibility. It’s helpful for adding a sleek, space-saving menu to your website.

The menu is ideal for navigation menus, saving screen space, and improving the overall design. This code helps create a modern, interactive menu without complex coding.

How to Create Menu and Show On Click  Event Using JavaScript

1. First of all, load the Normalize CSS and Font Awesome CSS (for icons) by adding the following CDN links into the head tag of your webpage.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.min.css'>

2. Create the basic HTML structure for your menu. Replace the links with your website URLs, you can add as many links as you want.

<div class="wrapper">
  <ul class="list">
    <li class="list-item"><a class="list-link" href="#"><i class="fas fa-camera"></i>Photo</a></li>
    <li class="list-item"><a class="list-link" href="#"><i class="fas fa-file"></i>Files</a></li>
    <li class="list-item"><a class="list-link" href="#"><i class="fas fa-microphone"></i>Record</a></li>
    <li class="list-item"><a class="list-link" href="#"><i class="fas fa-comment-alt"></i>New email</a></li>
  </ul>
  <button type="button"><i class="fas fa-paper-plane"></i><i class="fas fa-times"></i></button>
</div>

3. Now, use the following CSS code to style your menu. It’s responsible for the menu’s appearance and transition effects.

* {
  box-sizing: border-box;
  outline: none;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #fcdee7;
}

.wrapper {
  max-width: 70px;
  max-height: 70px;
  position: relative;
  background-color: #161722;
  color: white;
  border-radius: 35px;
  transition: all 0.4s ease-out 0s;
}
.wrapper.active {
  max-width: 500px;
  max-height: 300px;
  border-radius: 15px;
  transition: all 0.7s ease-in 0s;
}
.wrapper.active .list {
  opacity: 1;
  transform: scale(1);
  transition: all 0.3s ease-out 0.2s;
}
.cd__main{
  background: linear-gradient(to right, #ee9ca7, #ffdde1) !important;
  min-height: 720px;
}

.wrapper.active .list-link {
  opacity: 1;
}
.wrapper.active button {
  top: calc(100% + 10px);
  right: 0;
}
.wrapper.active button i.fa-paper-plane {
  top: 0%;
  left: 100%;
  opacity: 0;
  transition: all 0.5s ease-out 0s;
}
.wrapper.active button i.fa-times {
  opacity: 1;
  transform: translate(-50%, -50%) rotate(0);
  transition: all 0.3s ease-in 0.2s;
}

.list {
  list-style: none;
  margin: 0;
  padding: 15px 0;
  overflow: hidden;
  opacity: 0;
  transform: scale(0);
  transition: all 0.5s ease-out 0s;
}
.list-link {
  display: block;
  color: white;
  text-decoration: none;
  padding: 8px 30px;
  opacity: 0;
  transition: all 0.3s ease;
}
.list-link i {
  position: relative;
  left: 0;
  margin-right: 10px;
  transition: all 0.3s ease;
}
.list-link:hover i {
  left: -5px;
  color: #f04779;
}

button {
  width: 70px;
  height: 70px;
  position: absolute;
  top: calc(50% - 35px);
  right: calc(50% - 35px);
  background-color: #f04779;
  color: white;
  font-size: 22px;
  border-radius: 35px;
  border: none;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.5s ease;
}
button:hover {
  background-color: #ee2f68;
}
button i {
  position: absolute;
  top: 50%;
  left: 50%;
}
button i.fa-paper-plane {
  transform: translate(-50%, -50%);
  transition: all 0.2s ease-in 0.2s;
}
button i.fa-times {
  opacity: 0;
  transform: translate(-50%, -50%) rotate(-90deg);
  transition: all 0.3s ease-out 0s;
}

4. Finally, add the following JavaScript code between the <script> tag (or external JS file) just before closing the body element. It handles the menu’s functionality. It toggles the menu’s visibility when the button is clicked.

const button = document.querySelector('button');

const onClickButton = e => {
  e.preventDefault();

  const wrapper = e.currentTarget.closest('.wrapper');

  if (wrapper.classList.contains('active')) {
    wrapper.classList.remove('active');
  } else {
    wrapper.classList.add('active');
  }
};

// Init
const init = () => {
  button.addEventListener('click', onClickButton, false);
};

document.addEventListener('DOMContentLoaded', init);

That’s all! hopefully, you have successfully created a responsive menu that shows on click using JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X