Designs for an HTML5 Cheeseburger navigation interface

I've been struggling to implement a functional and visually appealing hamburger menu on my website. The challenge lies in integrating the menu seamlessly into the page rather than having it just pop up abruptly.

Despite trying various webkit tools, I can't seem to achieve the desired animation effect with the bar elements only triggering once.

Menu.style.display = "none";

function ShowHide(x) {
  x.classList.toggle("change");
  var Menu = document.getElementById("Menu");
  if (Menu.style.display === "none") {
    Menu.style.display = "block";
  } else {
    Menu.style.display = "none";
  }
}
.container {
  display: inline-block;
  cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
  width: 35px;
  height: 5px;
  background-color: #333;
  margin: 6px 0;
  transition: 0.4s;
}

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {
  opacity: 0;
}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}


}
body {
  background-color: rgb(209, 224, 224);
}
.Menu {
  width: 150px;
  background-color: rgb(208, 208, 225);
  display: none
}
<div class="container" onclick="ShowHide(this)">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>

<div id="Menu" style="width: 150px; background-color: rgb(208, 208, 225); display: none;">
  <h2><a href="Homepage.html">Homepage</a></h2>
  <br>
  <h2><a href="Subpage1.html">Subpage 1</a></h2>
  <br>
  <h2><a href="Subpage2.html">Subpage 2</a></h2>
  <br>
  <h2><a href="Subpage3.html">Subpage 3</a></h2>
  <br>
  <h2><a href="Subpage4.html">Subpage 4</a></h2>
  <br>
  <h2><a href="Subpage5.html">Subpage 5</a></h2>
  <br>

</div>

Answer №1

To ensure a smooth menu transition alongside the menu icon, you can integrate the following code snippet into your ShowHide() function. By introducing a new .menu-hidden class, you can easily toggle it without relying on conditional statements:

HTML:

<div id="Menu" class="menu-hidden">

JS:

function ShowHide(x) {
  x.classList.toggle("change");
  var Menu = document.getElementById("Menu");
  Menu.classList.toggle("menu-hidden");
}

If you wish for the menu to smoothly slide onto the page instead of simply being hidden with display: none, you can achieve this effect by initially translating it off the page using the below CSS rule:

.menu-hidden {
  transform: translateX(-999px);
}

Lastly, let's enhance your CSS code with a smooth transition (also ensuring that your selector is corrected from the .Menu class to the #Menu ID and moving the style from inline HTML to CSS):

#Menu{
  width: 150px;
  background-color: rgb(208, 208, 225);
  transition: 0.4s;
}

Moreover, for better separation between your JS and HTML, consider employing an event listener instead of the onclick attribute as shown below (though this step is optional):

var x = document.querySelector(".container");

x.addEventListener("click", ShowHide);

function ShowHide() {
  this.classList.toggle("change");
  var Menu = document.getElementById("Menu");
  Menu.classList.toggle("menu-hidden");
}

Below is the complete code:

var x = document.querySelector(".container");

x.addEventListener("click", ShowHide);

function ShowHide() {
  this.classList.toggle("change");
  var Menu = document.getElementById("Menu");
  Menu.classList.toggle("menu-hidden");
}
.container {
  display: inline-block;
  cursor: pointer;
}

.bar1, .bar2, .bar3 {
  width: 35px;
  height: 5px;
  background-color: #333;
  margin: 6px 0;
  transition: 0.4s;
}

.change .bar1 {
  -webkit-transform: rotate(-45deg) translate(-9px, 6px);
  transform: rotate(-45deg) translate(-9px, 6px);
}

.change .bar2 {opacity: 0;}

.change .bar3 {
  -webkit-transform: rotate(45deg) translate(-8px, -8px);
  transform: rotate(45deg) translate(-8px, -8px);
}
 
body {
  background-color: rgb(209, 224, 224);
}

#Menu{
  width: 150px; background-color: rgb(208, 208, 225);
  transition: 0.4s;
}

.menu-hidden {
  transform: translateX(-999px);
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="IST101.css">
    <script src="hamburger.js" defer></script>
    <title>Web design test</title>

</head>
<body>
    <div class="container">
  <div class="bar1"></div>
  <div class="bar2"></div>
  <div class="bar3"></div>
</div>

<div id="Menu" class="menu-hidden">
  <h2><a href="Homepage.html">Homepage</a></h2>
  <br>
  <h2><a href="Subpage1.html">Subpage 1</a></h2>
  <br>
  <h2><a href="Subpage2.html">Subpage 2</a></h2>
  <br>
  <h2><a href="Subpage3.html">Subpage 3</a></h2>
  <br>
  <h2><a href="Subpage4.html">Subpage 4</a></h2>
  <br>
  <h2><a href="Subpage5.html">Subpage 5</a></h2>
  <br>  

</div>
</body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message: Unable to access properties of null when calling 'registerPlugin' in @devexpress/dx-react-grid-material-ui

I have developed a CustomGrid component that is based on the DevExpress Grid component. So far, it has been working smoothly. Here's how the implementation looks like in App.tsx: import Paper from "@mui/material/Paper"; import { Table, TableHeaderRow ...

Protractor: Moving further down the page

One issue I encountered is with a button on my page that becomes visible only when the user scrolls down. As a result, Protractor tests are throwing an error: UnknownError: unknown error: Element is not clickable at point (94, 188). I attempted to reso ...

Automatic switching between Bootstrap 5 tab panes at precise time intervals

Is there a way to have the Bootstrap 5 tab-pane automatically switch between tabs at set intervals, similar to a Carousel? I found a code snippet on this link, but it appears to be outdated and only works for Bootstrap 3. Could someone assist me in updati ...

Tips for isolating data on the current page:

Currently, I am using the igx-grid component. When retrieving all data in one call and filtering while on the 3rd page, it seems to search through the entire dataset and then automatically goes back to "Page 1". Is there a way to filter data only within th ...

Basic Inquiry: Unhandled TypeError - Unable to access the property 'length' of an object that is not defined

Currently, I am utilizing a straightforward JSON Ajax request to fetch some JSON data. However, every time I attempt to use the JSON object, I encounter the following error: Uncaught TypeError: Cannot read property 'length' of undefined $(do ...

Tab buttons that switch between different sections with just a click

Forgive me if this seems like a simple coding issue, but I struggle with javascript and need some guidance... I have a setup where two buttons (blue and yellow) toggle between different content divs. On another part of the page, there are also two buttons ...

JavaScript overlay that does not interact with HTML elements directly

Currently, I am facing the challenge of implementing a javascript overlay upon page load without direct access to the html code. My only options are to upload .js and .css files. I have extensively searched for solutions, but as someone with limited exper ...

Tips for decreasing Vuetify Grid column width on larger screens

I'm currently utilizing the vuetify grid system to construct a reply component. The column ratio is set at 1:11 for the icon and reply columns, respectively. Below you'll find the code snippet: <v-row justify="center" class="ma-0 pa-0"> ...

Manipulating SVG image color using JavaScript

Is there a way to change the colors of an svg image using Javascript? Perhaps by loading it as an object and accessing the color/image data? I would greatly appreciate any responses or tips on this matter! ...

Creating an object property conditionally in a single line: A quick guide

Is there a more efficient way to conditionally create a property on an object without having to repeat the process for every different property? I want to ensure that the property does not exist at all if it has no value, rather than just being null. Thi ...

Updating the state of a nested array using React Hooks

After spending some time working with React Hooks, my main struggle has been dealing with arrays. Currently, I am developing a registration form for teams. Each team consists of a list of players (an array of strings). The goal is to allow users to add t ...

JavaScript Loading Screen - Issues with window.onload functionality

I am currently working on creating a loading screen for my project. I need to use JavaScript to remove the CSS property "Display: none" from the page, but for some reason, my code is not functioning as expected. The Issue: I discovered that using window. ...

Unexpectedly, the NodeJS application experiences a crash following numerous requests

I can't seem to figure out why my Nodejs app crashes after a certain number of requests (currently 22). The issue arises with this simple get request handler: router.get('/api/docs/fetch', async (req,res) => { try{ let docs = ...

Space left vacant following an unordered list item

Is there a way to remove unwanted space after an unordered list in IE and Firefox, specifically in the last HTML <li> item? I've tried adjusting the ul width but it didn't work. Any ideas on how to get rid of the space? Thanks. #menubar ...

Having Trouble with Form Submission Button Across Different Web Browsers

Having some trouble with my form - all fields are properly closed with tags, but when I click the submit button, nothing happens. The page is loaded with code, so here's the link for you to check it out. Unfortunately, right-click is disabled, so ple ...

A simple demo showcasing interactive HTML pages using Django and AJAX

In my search for answers, I came across the following helpful posts: How to Reload table data in Django without refreshing the page Tutorial on Django dynamic HTML table refresh with AJAX Despite the insights from these posts, I am still facing chal ...

Having trouble with textures when using THREE.TextureLoader instead of the outdated THREE.ImageUtils.loadTexture?

Recently, I utilized a function to apply texture on a Cylinder shape. function createElementMaterial() { THREE.ImageUtils.crossOrigin = ''; var t = THREE.ImageUtils.loadTexture( IMG_MACHINE ); t.wrapS = THREE.RepeatWrapping; t.wr ...

Encountering Unmet Dependency Issue When Running 'npm install' on Laravel in Windows 8

Currently, I am in the process of working on a Laravel project and looking to utilize Elixir to handle my front-end tasks. After running 'npm install', I encountered a warning stating "npm WARN unmet dependency". What steps should I take in order ...

Tips for Integrating a Facebook Shop Page into Your Website

Can a Facebook shop page be integrated into a website? Any guidance on how to accomplish this task would be greatly valued. ...

A JSON object received from an AJAX request is either null or empty

I recently deleted a previous question that I had posted because it was no longer relevant and there was another issue to address. The response provided below is very clear, more so than other responses, and I believe it will be beneficial for anyone else ...