Apply a Fade In transition to the ul li elements following a JavaScript toggle

I'm currently experimenting with implementing a Fade in from the right animation for the links displayed in the menu after toggling the menu body in the browser. Despite my efforts, I am unable to determine why the animation is not functioning as expected. I have attempted placing the animation in various sections of the ul and even tried adding a classlist without success. It's possible that I may be inserting the animation in the incorrect element, but I'm uncertain. You can view an example of the animation effect I desire for the links here: https://codepen.io/Fafnir/pen/mvVyRz. My primary focus is on achieving the FadeInRight animation rather than the underline link style.

var toggleStatus = 1;
   
if (toggleStatus == 1) {
document.getElementById("menu").style.left = "-5px";
    
       
        //The Burger Menu icon is called toggleMenu
        document.getElementById("toggleMenu").style.left = "200px";
        
toggleStatus = 0;
        
        
        //This section will execute another function when clicked again, essentially toggling back to initial state.
} else if (toggleStatus == 0) {
document.getElementById("menu").style.left = "-245px";

        
        document.getElementById("toggleMenu").style.left = "5px";
        toggleStatus = 1
}
}
* {
margin: 0;
padding: 0;
text-decoration: none;
}

body {
background-color: grey;
}

/*Utilizing position methods like left, right, top, and bottom to dictate the menu appearance direction.*/
#menu {
width: 240px;
background-color: orange;
position: fixed;
top: 0;
bottom: 0;
left:-250px;
z-index: 1000;
 transition: all ease-in-out 200ms;
-webkit-transition: all ease-in-out 200ms;
border-right: 2px solid black;
  width: 240px;
  height:350px;
 overflow:scroll;
 overflow: -moz-scrollbars-none;
  -ms-overflow-style: none;
overflow: -moz-scrollbars-none;
 
  
 
}

#menu::-webkit-scrollbar {
  width: 0 !important 
}

#menu img {
display: block;
width: 10%;
margin: 0 auto;
padding-top: 50px;
}

#menu ul {
padding-left: 30px;
padding-top: 35px;
  
}

#menu ul li {
list-style: none;
padding: 4px 0px;

  -webkit-animation: fadeInRight .5s ease forwards;
          animation: fadeInRight .5s ease forwards;
  -webkit-animation-delay: .35s;
          animation-delay: .35s;
 
}

#menu ul li a {
font-family: Arial;
font-weight: 300;
color: #272727;
text-transform: uppercase;
}


#toggleMenu {
width: 20px;
height: 20px;
background-color: #fff;
background-image: url(https://static.thenounproject.com/png/195031-200.png);
  background-size:cover;
position: fixed;
top: 0px;
left: 5px;
z-index: 1050;
cursor: pointer;
border: 10px solid #fff;
border-radius: 2px;
transition: all ease-in-out 200ms;
-webkit-transition: all ease-in-out 200ms;
}

#toggleMenu:hover {
opacity: 0.7;
}

@-webkit-keyframes fadeInRight {
  0% {
    opacity: 0;
    left: 20%;
  }
  100% {
    opacity: 1;
    left: 0;
  }
}

@keyframes fadeInRight {
  0% {
    opacity: 0;
    left: 20%;
  }
  100% {
    opacity: 1;
    left: 0;
  }
}
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="toggleMenu"></div>
  
<div id="menu">
    
<img src="https://cdn3.dualshockers.com/wp-content/uploads/2011/11/Master-Sword-and-Triforce.jpg">
<!--**MAYBE ADD A CLASS TO UL AND CHANGE NAV ID-->
    <ul>
<li><a href="#">Home</a></li>
<li><a href="#">Cases</a></li>
<li><a href="#">Personal projects</a></li>
<li><a href="#">About me</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>


</body>
</html>

Answer №1

If I comprehend your query correctly, the primary concern here involves utilizing animation for achieving the fade-in effect. Instead of using animation, consider substituting it with transition to accomplish the desired outcome:

#menu ul li {
  list-style: none;
  padding: 4px 0px;

  /*
  Specify initial state and transition for menu li
  */
  opacity:0;
  transform:translateX(-25px); 

  /*
  Use transition instead of animation
  */
  transition: all 1.5s ease;
}

Furthermore, contemplate modifying your menu structure by incorporating CSS classes rather than directly setting inline styles (e.g., via style.left = "200px"). You can determine whether the menu is toggled by checking for the presence of a "toggle" class on the menu element itself instead of relying on an external variable (e.g., toggleStatus):

if(menu.classList.contains('toggled')) {
    /* Menu has 'toggled' class indicating that it is toggled */
}
else {
    /* Menu does not have 'toggled' class indicating that it is not toggled */
}

This approach offers several benefits; besides rendering the toggleStatus variable unnecessary, you can expand your CSS to ensure that the toggle class indirectly triggers the "fade-in" transition behavior you desire for li elements to display as illustrated below:

/* 
The transformation and opacity changes to li only apply when the parent
menu has the 'toggled' class applied
*/
#menu.toggled li {
  transform:translateX(0px);
  opacity:1;
}

For more detailed insights into this methodology, refer to the comments provided within the code snippet below:

const toggleMenu = document.getElementById('toggleMenu');

/*
Iterate through each li element within #menu, calculate a transition delay, and apply it directly to
each li element based on its index (creating a staggered effect)
*/
document.body.querySelectorAll('#menu li').forEach((li,index) => {

    li.style.transitionDelay = (0.1*index) + 's';
})

toggleMenu.addEventListener('click', () => {
 
  /*
  Retrieve the menu element for usage in either toggle case
  */
  const menu = document.getElementById("menu");

  /* 
  Consider employing a CSS class and the contains() method to track 
  state without relying on the toggleStatus variable
  */
  if (menu.classList.contains('toggled')) {
    
    /*
    Remove the 'toggled' class (modifier) from both the menu and toggleMenu elements
    when the menu is toggled
    */
    menu.classList.remove('toggled');
    toggleMenu.classList.remove('toggled');
    
  } else {
  
    /*
    Add the 'toggled' class (modifier) to both the menu and toggleMenu elements
    when the menu is not toggled
    */
    menu.classList.add('toggled');
    toggleMenu.classList.add('toggled');
  }

});
/*
Specify the 'toggled' state for the menu
*/
#menu.toggled {
  left:-5px;
}

/*
Define the final animated state for menu
items (li elements) when toggled
*/
#menu.toggled li {
  transform:translateX(0px);
  opacity:1;
}

...
... Code snippets continue ...

I trust this guidance proves beneficial!

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

removing duplicate items from an array in Vue.js

I created a Pokemon App where users can add Pokemon to their 'pokedex'. The app takes a pokemon object from this.$store.state.pokemon and adds it to this.$store.state.pokedex. However, users can add the same pokemon multiple times to the pokedex, ...

There seems to be a mysterious absence of chuck norris jokes in

I'm attempting to call two different APIs by clicking a button to display a random joke on the screen. I've created two separate functions to fetch the APIs and used Math.random() to generate a random number. If the number is greater than 50, one ...

Issue with reflect metadata in Next.js edge runtime causing functional problems

Currently, I am utilizing a package in my upcoming 13 app that incorporates reflect metadata. Unfortunately, during the next build process, an error occurs for which I haven't been able to find a solution. ../../eshop-sdk-js/node_modules/reflect-metad ...

Is there a way for me to determine when AJAX-loaded content has completely loaded all of its images?

After making an AJAX request to load HTML, it includes img tags within it. I am in need of a way to detect when these images have finished loading so that I can adjust the container size accordingly. Since I do not know how many images will be present in ...

What is the best way to create direct links to tabs using #anchors in JavaScript?

I am managing a website that includes info-tabs based on a template I found at this link. The tabs contain anchors like example.com/#convey, but unfortunately, direct links to specific tabs using #anchor do not work properly due to the tabs relying on Java ...

Using JavaScript, HTML, and CSS to select slices of a donut pie chart created with SVG

I have successfully implemented CSS hover effects and can manipulate the HTML to use the .active class. However, I am facing difficulties in making my pie slices switch to the active state upon click. Moreover, once this is resolved, I aim to enable select ...

"Enhance your website with autocomplete feature using the power of jQuery 1.4.2 and jQuery UI 1

Struggling to make jQuery autocomplete work. Despite searching for examples, most seem outdated. Modeled after a good example from the jQuery UI site but still can't get it to display data. My JSON data source is accessed via URL. [{ "pk": 1, "mo ...

Exploring the data-* attribute in jQuery

Currently, I have a form structured as follows: <div data-role="page" data-last-value="43" data-hidden="true" data-bind='attr : {"name":"John"}'></div> My objective is to modify the name attribute from "John" to "Johnny". I am att ...

Issue encountered while attempting to attach an event listener to an element within a class

Upon running the code, I encountered an error message that reads: Uncaught TypeError: this.startButton.addEventListener is not a function and I'm unsure of how to resolve it. Although I can successfully console.log the button inside the class, adding ...

Video playback disabled on hover in Chrome browser

I have added videos to a search results page. When the user hovers over the video, I want to show a preview of it in a separate container and start playing the video. $(".videoSearchResults video").mouseenter(function () { var container = document.cre ...

Seeking a quick conversion method for transforming x or x[] into x[] in a single line of code

Is there a concise TypeScript one-liner that can replace the arrayOrMemberToArray function below? function arrayOrMemberToArray<T>(input: T | T[]): T[] { if(Arrary.isArray(input)) return input return [input] } Trying to cram this logic into a te ...

How to position slides in the center using react-slick

I'm having difficulty achieving vertical centering for an image in a react-slick carousel implementation. The issue can be seen here. https://codesandbox.io/s/react-slick-playground-o7dhn Here are the problems identified: Images are not centered: ...

Managing Emails with Vue and Firestore

I am facing an issue with updating the 'email' field. Whenever I try to change the email address, it gets updated correctly. However, when I attempt to log in again, the new email address does not work; only the old one seems to be functional. Ho ...

I am facing difficulties retrieving the JSON object returned from the Express GET route in Angular

After setting up an express route, the following JSON is returned: [{"_id":"573da7305af4c74002790733","postcode":"nr22ry","firstlineofaddress":"20 high house avenue","tenancynumber":"12454663","role":"operative","association":"company","hash":"b6ba35492f3 ...

Is a CSS-only autoexpanding label possible within a list?

I am interested in having all the labels automatically expand to the size of the widest one. Below is the HTML structure: <div class="body"> <ul class="list"> <li> <span> <label>condition</label> ...

Update overall font size to be 62% for a consistent look across the website

Recently, I developed a browser extension that adds an overlay button to the LinkedIn website. Everything was running smoothly until I discovered that LinkedIn uses a global font-size: 62,5% that completely messes up my design.. https://i.stack.imgur.com ...

What is the best way to allow someone to chain callback methods on my custom jQuery plugin?

My goal is to enhance the functionality of jQuery.post() by implementing a way to check the response from the server and trigger different callbacks based on that response. For instance: $("#frmFoo").postForm("ajax") .start(function () { showSpinner( ...

Encountering a script error that reads "TypeError: $ is not defined as a function

I am attempting to send an email using web services in asp.net, utilizing html and sending the information through a jquery ajax function. Below is the html form: <div class="col-md-6"> <h2>DROP ME A LINE</h2> & ...

When useEffects are linked in such a way that the first one updates a dependency of the second, it can lead to

Modified code block with console logs integrated from approved solution. I have stripped down a search bar component to its simplest form: import React, { useEffect, useRef, useState } from "react"; import axios from "axios"; const re ...

What is the best way to verify the status codes of multiple URLs using JavaScript?

In my JavaScript project, I am currently checking the status codes of various URLs. While I can successfully check the status for one URL at a time by writing the code manually, I am facing an issue as I have over 100 URLs to check. This is making my cod ...