What could be the reason my CSS and Javascript animation is not functioning properly?

Currently working on creating a dynamic animation menu using CSS and Javascript, but encountering some issues. The concept involves having a menu with initially hidden opacity (using CSS), which becomes visible when the hamburger menu is clicked, sliding in from the right slightly (powered by Javascript). While the menu slides in properly, upon clicking the X button to close it, the menu does not slide out and the X icon fails to toggle back to the hamburger icon.

Would utilizing a CSS transition instead of an animation be a more straightforward approach?

Below is the code snippet:

let d = document.getElementsByClassName("show")[0];
let f = document.getElementsByClassName("menu")[0];
let o = document.getElementsByClassName("burger")[0];
let p = document.getElementsByClassName("burgers")[0];

d.toggleStatus = "off";

f.onclick = function() {
  switch (d.toggleStatus) {
    case "off":
      d.toggleStatus = "on";
      d.setAttribute('style', 'animation-play-state:running');
      break;

    case "on":
      d.toggleStatus = "off';
      d.setAttribute('style', 'display:none');
      break;
  }
  o.classList.toggle('rotate');
  p.classList.toggle('rotate2');

}
.show {
  opacity: 0;
  animation: menu .3s ease-out;
  animation-play-state: paused;
  animation-fill-mode: forwards
}

@keyframes menu {
  0% {
    margin-right: 190px;
    opacity: 0
  }
  100% {
    margin-right: 89px;
    opacity: 1
  }
}
 <script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp"></script>
<div class="menu">
  <div class="burger"><img src="https://picsum.photos/50" /></div>
  <div class="burgers"><img src="https://picsum.photos/50" /></div>
</div>
<div class="show pt-20 min-w-screen text-center min-h-screen"><a href="#"><span>&Home</span></a><a href="#"><span>About</span></a><a href="#"><span>Work</span></a><a href="#"><span>Contact</span></a>

For optimal viewing experience, please access the page in full screen mode.

Answer №1

You have the option to incorporate a transition paired with toggling classes.

const menu = document.querySelector(".menu");
const burgerContainer = document.querySelector(".burger-container");
const burgerIcon = document.querySelector(".burger");
const burgers = document.querySelector(".burgers");

burgerContainer.addEventListener('click', function() {
  menu.classList.toggle('show');
  burgerIcon.classList.toggle('hidden');
  burgers.classList.toggle('hidden');
  burgerIcon.classList.toggle('rotate');
  burgers.classList.toggle('rotate2');
});

console.clear();
.menu {
  opacity: 0;
  transition: all .3s ease-out;
  margin-right: 190px;
}
.menu.show {
  opacity: 1;
  margin-right: 89px;
}
<script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio,line-clamp"></script>
<div class="burger-container">
  <div class="burger"><img src="https://picsum.photos/50" /></div>
  <div class="burgers hidden"><img src="https://picsum.photos/40" /></div>
</div>
<div class="menu pt-20 min-w-screen text-center min-h-screen">
  <a href="#"><span>Home</span></a>
  <a href="#"><span>About</span></a>
  <a href="#"><span>Work</span></a>
  <a href="#"><span>Contact</span></a>
</div>

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

"Create sleek and stylish forms with Bootstrap's horizontal

I am attempting to create a horizontal form in Bootstrap using the code provided in their documentation. The example they show has input boxes filling the entire column space, but for some reason this is not happening when I try it, even after copying the ...

Limit image size in Outlook when using Mailchimp

I'm currently working on coding a Mailchimp template for a client and I've run into an issue with image dimensions. The problem arises when images exceed the width of the template (usually 600px), as they are displayed at their original size in ...

Could you provide me with a demonstration of cross-domain functionality?

Similar Inquiry: Methods to bypass the same-origin policy Let's consider two domains for this example - "" and "". The first domain "" is generating data in JSON format as shown below: { "str_info": [ { "str_name": "Mark ...

What is the reason for multiple ajax functions being triggered when submitting a form through ajax?

I have a Drupal form with an AJAX submit. Additionally, I have another jQuery $.get function that sends a request every 2 minutes and inserts the response into an HTML element. The form and this JavaScript code are independent of each other, performing sep ...

The prop HandleClick is not being identified

Whenever I click on the sidebar, my menu should appear, but instead I'm encountering an error message saying "react-dom.development.js:86 Warning: React does not recognize the handleClick prop on a DOM." import { useState } from "react"; imp ...

Fetching JSON data from an external URL using AngularJS

Check out this URL that shows JSON data in the browser: I attempted to store the data in a variable with the following code: $http.get('http://api.geosvc.com/rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode& ...

Navigate to a different web page within the body tag without changing the current URL by utilizing a hash symbol

I am working with two HTML files, index.html and about.html. My goal is to display the content of about.html within my index.html without navigating away from it. After observing many websites, I noticed that they often use #about in their URLs to dynamic ...

What is the best way to determine the position of the currently chosen selection in relation to the top

Let's say we have a situation like this: <select id="my-select"> <option id="iun">Option 1</option> <option id="sdd">Option 2</option> <option id="ert" select="selected">Option 3</option> <option i ...

Learn how to keep sessionStorage state synchronized across ReactJS components

Within my application, there is a React component responsible for displaying a list of numbers while also keeping track of the total sum of these numbers using sessionStorage. Additionally, another component provides an <input /> element to enable u ...

Can a div section be defined and then filled with data in the same document?

Is it possible to initially declare a div section and later populate it with content in the same document? The following dummy code snippet illustrates the query... <div name="xyz" style="..."> <!-- Empty --> </div> < ... INTO "x ...

Unveiling the hidden secrets of HTML code through scraping

Looking to extract data from the cadastral records in Poland available at . Specifically, I am interested in retrieving information from the element tagged with id 'gfi_0'. The challenge is that this element is not immediately accessible; it only ...

Is the rotate() method in SVG supported on web browsers when passing in additional parameters like angle, centerX, and centerY similar to the CSS rotate

I have recently learned about a rotate transform that allows for rotating around a specified center point using the `rotate(angle, centerX, centerY)` method. However, I have noticed that this method does not seem to work when applied via CSS. Interestingl ...

Displaying conflicts in a single page by clicking on a checkbox

When I click on the <thead> checkbox of the first Table, it also checks the 2nd Table checkbox. However, that is not what I need. When I click on the First thead checkbox, all checkboxes in the first Table should be checked. Also, when I click on Fi ...

Tips for monitoring input content "live"

Currently, I am in the process of developing a web form that includes a text field intended to receive numeric values. If the user enters non-numeric characters into this field, the form will not submit. However, there is no error message displayed to noti ...

Location of Ajax callback function

I encountered an issue with a callback function that was placed within $(document).ready. The callback function wasn't functioning as expected. Surprisingly, when I moved it outside of $(document).ready, the code started working flawlessly. I am puzzl ...

Ensure that the user's credentials are accessible to all views in Node.js

I'm currently developing an Android application that utilizes Node.js web services. The initial interface requires the user to connect to a host using an IP address, login, and password in order to access all databases. I aim to save these credentials ...

Creating my website with a unique inverse color scheme

I'm looking to change the color scheme of my webpage so that it is inverse (white becomes black and black becomes white) similar to the Dark Reader chrome extension: https://chrome.google.com/webstore/detail/dark-reader/eimadpbcbfnmbkopoojfekhnkhdbiee ...

The sum is being treated as a concatenation instead of an addition in this case

Why is the somma value showing the concatenation of totaleEnergetico and totaleStrutturale instead of a sum? RiepilogoCombinatoStComponent.ts export class RiepilogoCombinatoStComponent implements OnInit { constructor() { } interventi: AssociazioneI ...

How can I place this ribbon on top of an image in an HTML email to ensure it displays correctly on email clients such as Outlook?

As I delve into my research, it's becoming apparent that achieving this result might not be feasible across all email clients. However, I'm curious to know if there have been any recent developments in the email world that would allow me to repli ...

Choose up to three elements using jQuery

DEMO I'm currently working on a project where I need to select only 3 items at a time. However, all the elements are being selected instead. Can someone please provide guidance on how to achieve this? "If a user wants to select another element, th ...