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

Switching the background image of a div by clicking on a different div

To start, you can locate all of my code right here. http://jsfiddle.net/yfukm8kh/1/ The issue I'm encountering pertains to the following section. var changePic = function (direction, id, array) { var intID = parseInt(id); var intDir = pars ...

Retrieving all selected checkboxes in AngularJS

I am a beginner in angular js and here is my template: <div class="inputField"> <h1>Categories</h1> <div> <label><input type="checkbox" id="all" ng-model="all" ng-change="checkAll();" ng-true-value="1">A ...

Creating a redux store with an object using typescript: A step-by-step guide

Having recently started using Redux and Typescript, I'm encountering an error where the store is refusing to accept the reducer when working with objects. let store = createStore(counter); //error on counter Could this be due to an incorrect type set ...

What could be causing my header component to rerender even when there are no new props being received?

https://codesandbox.io/s/crimson-field-83hx6 In my project, I have a Header component that simply displays a fixed text and includes a console.log statement: const Header = props => { console.log("header render"); return ( <header> ...

Steps for uploading an item to an API using an Excel document

I'm facing an issue where I am trying to send a large object along with an Excel file to an API. However, only my photo is being recognized and the object is not sent, resulting in an [object Object] error. I understand that this error occurs due to i ...

"Customize the number of items displayed per page with Bootstrap Vue's perPage

I am currently working on a Vue project which you can view on codesandbox and utilizing bootstrap-vue. Within the project, there are multiple columns containing cards along with pagination: <template> <b-container> <b-row :cu ...

What is the best way to pass a variable between different routing functions?

I am currently developing a server-side parser for an API. Each GET request made to my website must first initiate a request to the API, and since this request is always the same, I would like to encapsulate it within its own function. What is the best wa ...

Remove the color gradient for the column headers in the Google Visualization table

Whenever I attempt to change the colors of the column headers using the method demonstrated in this insightful source, a rather generic gradient is applied. Interestingly, the example code provided also demonstrates the same default gradient on the secon ...

The type unknown[] cannot be assigned to type React.ReactNode

import * as React from 'react'; import { List, ListItemButton, ListItemIcon, ListItemText, ListItem} from '@mui/material'; import LightbulbOutlinedIcon from '@mui/icons-material/LightbulbOutlined'; import NotificationsNoneOutl ...

What could be causing React onclick events to not trigger when wrapped within a Vue application? (No additional libraries)

As I dive into the world of combining React and Vue components, I encountered an interesting challenge... const RootTemplate = () => { return ( <div id="vue-app"> ... <IconButton color="inherit" onClick={thi ...

Customize the MUI (JoyUI) Autocomplete feature in React using react-hook-form to display a unique value instead of the label

Currently, I am in the process of creating a form that includes JoyUI (material) autocomplete and react-hook-form functionality. The array of objects I am using for my options looks like this: [ { label: "Todo", param: "TODO" }, ...

What is the best way to justify list items to the left?

Having trouble aligning my list content to the left. Any suggestions are welcome! Here is a snippet of the code: <div class="col-md-4 mb-1"> <i class="fas fa-hiking fa-4x"></i> <h4 class="my-4" font-weight-bold&g ...

Modify the contents of an array within a string using JavaScript

let message = "hello https://ggogle.com parul https://yahoo.com and http://web.com"; let url = ["https://ggogle.com", "https://yahoo.com", "http://web.com"]; I'm trying to replace all URLs in the 'message' array with "***" from the 'ur ...

Struggling with Vue's Router Transition fade in/out effect not functioning as expected?

Question: I've implemented Vue's Router and it switches between components without any issues. However, I added a <transition name="fade" mode="out=in"> around it but the fade effect is not working as expected. Desired ...

What is the process of creating a MaterialUI checkbox named "Badge"?

Badge API at https://material-ui.com/api/badge/ includes a prop called component that accepts either a string for a DOM element or a component. In my code: <Badge color="primary" classes={{ badge: classes.badge }} component="checkbox"> <Avatar ...

Difficulty Loading Static JavaScript File in Express.js

Currently in the process of setting up an express server with create-react-app. Encountering this error in the console: Uncaught SyntaxError: Unexpected token < bundle.js:1 Upon clicking the error, it directs me to the homepage htm ...

Stylish CSS typography options for both Mac and Windows users

When browsing various websites, I've noticed that many of them showcase beautiful fonts without using images. Examples include The Firebug site and Happy Cog. It seems like web designers use CSS tricks and JavaScript to achieve this effect. I'm ...

What is the best way to integrate ES6 ReactJS code into an Express application?

I am trying to initially render my ReactJS application on the server using ExpressJS. Although I have been able to import ES6 modules using require(), the module crashes upon loading because it contains ES6 code (ES6 import and export). Index Route var ...

Step-by-step guide on positioning an image to show at the bottom of a div

I am trying to create a div that covers 100% of the screen height, with a table at the top and white space below it for an image. However, when I add the image, it ends up directly under the table instead of at the bottom of the DIV. I have searched on G ...

Update the image with a new one using jQuery and PHP, then refresh the page to see

I'm currently utilizing simpleImage, a PHP image manipulation library. My aim is to externally rotate an image using AJAX and replaceWith functions. It successfully replaces the image, but unfortunately it doesn't refresh after rotation. Here&ap ...