Is there a way to keep a section of an animated class in place?

My website features an animated retractable menu bar with two separate menus. Clicking on one icon activates a sliding motion to reveal that particular menu while causing the second menu to retract in a similar fashion. To achieve a smooth transition effect, I utilize the hidden-sidenav class to adjust the delay time for the closing navigation, allowing the expanding menu to wait until the retraction is completed before appearing.

One aspect of the transition that I'm not entirely satisfied with involves the movement of the icon itself. This is primarily due to my use of the box-sizing property and padding for each individual menu bar. Although I employ box-sizing to center the icon within the bar, I am aiming for a more seamless animation similar to the example provided here. Notice how the links in the navigation bar remain fixed in place.

function closeIt(){
  document.getElementById('mysidenav').classList.add('hidden-sidenav');
  document.getElementById('mysidenav2').classList.remove('hidden-sidenav');
}
function openIt(){
  document.getElementById('mysidenav').classList.remove('hidden-sidenav');
  document.getElementById('mysidenav2').classList.add('hidden-sidenav');
}
*{
  margin:0;
  padding:0;
}
html,body{
  height:100%;
  width:100%;
}
.sidenav{
  height:100%;
  width:20%;
  background:#111;
  transition:1s;
  transition-delay:1s;
  transition-timing-function:ease-out;
  overflow-x:hidden;
  box-sizing:border-box;
  padding:calc((20% - 50px)/2);
}
.sidenav a{
  font-size:90px;
  color:#818181;
}
/*SECOND SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDI BAR*/
.sidenav2{
  height:100%;
  width:20%; 
  background:#111;
  overflow-x:hidden;
  position:fixed;
  top:0;
  transition:1s;
  transition-timing-function:ease-out;
  transition-delay:1s;
  box-sizing:border-box;
  padding:calc((20% - 50px)/2);
}
.sidenav2 a {
  font-size:50px;
  color:#818181;
}
.hidden-sidenav { 
  transition-delay:0s;
  transition-timing-function:ease-in;
  width:0;
  padding:0;
}
<div id='mysidenav'class='sidenav hidden-sidenav'>
  <a onclick='closeIt()'>&times</a>
</div>
    
<div id='mysidenav2'class='sidenav2'>
  <a onclick='openIt()'>&#9776</a>
</div>

Answer №1

To keep the icons fixed in position, I simply included

position:absolute;
left:15px;

within

.sidenav a
.sidenav2 a

I also eliminated padding calculations and changed the positions to absolute. Additionally, to account for font size variations, I added

top:12px;

to

.sidenav2 a

Expecting this information will be of assistance.

function closeIt(){
  document.getElementById('mysidenav').classList.add('hidden-sidenav');
  document.getElementById('mysidenav2').classList.remove('hidden-sidenav');
  
}
function openIt(){
  document.getElementById('mysidenav').classList.remove('hidden-sidenav');
  document.getElementById('mysidenav2').classList.add('hidden-sidenav');
}


setIconPositions('mysidenav');
setIconPositions('mysidenav2');

function setIconPositions(parentElement) {
var element = document.getElementById(parentElement);
var positionInfo = element.getBoundingClientRect();
var width = positionInfo.width;

var closeIcon = document.getElementById('close-icon');
var openIcon = document.getElementById('open-icon');

closeIcon.style.left = (width/2-getWidthOfText(closeIcon.text, window.getComputedStyle(closeIcon).fontFamily,
window.getComputedStyle(closeIcon).fontSize)/2)+"px";

openIcon.style.left = (width/2-getWidthOfText(openIcon.text, window.getComputedStyle(openIcon).fontFamily,
window.getComputedStyle(openIcon).fontSize)/2)+"px";
}

function getWidthOfText(txt, fontname, fontsize){
    if(getWidthOfText.c === undefined){
        getWidthOfText.c=document.createElement('canvas');
        getWidthOfText.ctx=getWidthOfText.c.getContext('2d');
    }
    getWidthOfText.ctx.font = fontsize + ' ' + fontname;
    return getWidthOfText.ctx.measureText(txt).width;
}
*{
  margin:0;
  padding:0;
}
html,body{
  height:100%;
  width:100%;
}
.sidenav{
  height:100%;
  width:30%;
  background:#111;
  transition:1s;
  transition-delay:1s;
  transition-timing-function:ease-out;
  overflow-x:hidden;
  box-sizing:border-box;
  position:absolute;
  top:0;
}
.sidenav a{
  font-size:90px;
  color:#818181;
  position:absolute;
}


/*SECOND SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIDDDDDDDDDDDDDDDDDDDDEEEEEEE BAR*/
.sidenav2{
  height:100%;
  width:30%; /* Changed to 20%: visible by default. */
  background:#111;
  overflow-x:hidden;
  position:absolute;
  top:0;
  transition:1s;
  transition-timing-function:ease-out;
  transition-delay:1s;
  box-sizing:border-box;
}
.sidenav2 a {
  font-size:50px;
  color:#818181;
  top:12px;
  position:absolute;
}
.hidden-sidenav { /* Must come after .sidenav and .sidenav2 to override them. */
  transition-delay:0s;
  transition-timing-function:ease-in;
  width:0;
  padding:0;
}
<div id='mysidenav'class='sidenav hidden-sidenav'>
  <a id="close-icon" onclick='closeIt()' class='hidden-sidenav'>&times</a>
</div>
    
<div id='mysidenav2'class='sidenav2'>
  <a id="open-icon" onclick='openIt()' class='hidden-sidenav'>&#9776</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

Python Flask server struggling to find JavaScript and CSS files within index.html when paired with AngularJS frontend

I'm currently working on a simple website project that involves a Python backend running on Flask and an AngularJS frontend. The main issue I am facing is encountering 404 errors when the index template tries to load CSS and JS files. Here's how ...

The alignment of images loop and videos loop is off

https://i.stack.imgur.com/6o38F.jpg I am facing an issue with a td container in which I am using PHP to loop through a collection of videos and images. Strangely, the videos are displaying higher than the images even though they are both contained within ...

Refresh a particular element using javascript

I'm new to Javascript and I am trying to refresh a specific element (a div) using only Javascript when that div is clicked. I came across the location.reload() function, but it reloads the entire page which doesn't fit my requirements. Upon clic ...

Storing website data for localization purposes on the web platform

I am currently working on a project to develop a website that displays the selected language page upon first visit. The idea is that when a user clicks on a language name, such as French, it will be stored in web/local storage. Then, when the user returns ...

Retrieve the website address from a string of HTML code

I'm struggling to extract the URL from a given String that contains an HTTP response with an HREF tag. I've managed to identify the start of the links, but I need to stop the string as soon as the HREF tag ends. How can I accomplish this? public ...

Which HTML/CSS class should be chosen to pair with another class for compatibility with IE 6, 7, and 8?

I am struggling to create a clear div box that works seamlessly across various browsers such as Mozilla, Chrome, Opera, IE 9+, and Safari, including older versions like IE 6, 7, and 8. I have achieved success in all browsers except for IE 6, 7, and 8. To ...

Use markdown links instead of html hyperlinks

I encountered a scenario where I have a string formatted like this: 'Hello, my name is <a href="https://stackoverflow.com/foo/bar">foobar</a> My favorite food is <a href="https://google.com/food/pizza">pizza</a ...

You cannot directly access an array useState by index in React js, but you can use the map function to

export interface JobListing { id: number, isTraded: boolean, feedback: string, title: string, message: string, skills: string[], sender: string, ipAddress: string } const GroupList = () => { const [jobListings, setJobListings] = useSt ...

Is it normal for Tailwind animation to loop twice when transitioning between pages in Next.js?

I'm currently utilizing react-hot-toast for displaying alerts and animating them during page transitions. The animation involves a double fade-in effect when transitioning between pages. In my project, I've integrated tailwindcss-animate within ...

Bring in a function by its name from the ts-nameof package that is not declared in the d.ts export

Recently, I came across a captivating package that caught my interest and I would love to incorporate it into my TypeScript application: https://github.com/dsherret/ts-nameof However, upon attempting to import the nameof function, I realized it was not be ...

What is the reason that the Mongoose updateMany() function only functions properly when combined with .then

Currently delving into the intricacies of Mongoose, I am perplexed as to why updateMany() requires a .then() at the end in order to be executed. For instance, here is the snippet that I'm endeavoring to execute, with the intention of altering the rat ...

Error message: Electron is unable to read properties of undefined, specifically the property 'receive'. Furthermore, the IPC is unable to receive arguments that were sent through an HTML iframe

I am currently working on passing light mode and language data from ipcMain to ipcRenderer via my preload script: Preload.js: const { contextBridge, ipcRenderer } = require("electron"); const ipc = { render: { send: ["mainMenuUpdate& ...

Is it possible to hide the <dd> elements within a <dl> using knockout's custom data binding upon initialization?

I have implemented a <dl> where the <dd> can be expanded/collapsed by clicking on the corresponding <dt> using knockout's data binding. The inspiration for my solution came from a tutorial on creating custom bindings. Currently, I h ...

NextAuth: JWT callback that returns an object

I've been working on a project using Next.js (11.1.2) + NextAuth (^4.0.5) + Strapi(3.6.8). The Next Auth credentials provider is functioning correctly. However, I need to access certain user information using the session. I attempted to do this by ut ...

Utilizing HTML5 Drag and Drop feature to track the initial position of the element being dragged

Currently, I am utilizing the HTML 5 Drag and Drop API to create a sortable list with auto scroll functionality. One crucial aspect I am trying to incorporate is the ability to detect which specific part of an element was grabbed by the user. Take a look ...

JWT refresh and access tokens: A dynamic duo for secure authentication

In my project, I utilize jwt tokens with a strategic approach. Long-lived refresh tokens are employed for authentication purposes, while short-lived access tokens are used to safeguard protected resources. To enhance security against xss attacks, the refre ...

Best practices for implementing the map function with TypeScript

I'm currently working on mapping types in a DB using the Map function in JavaScript. This is my first time trying to do this, and I'm eager to learn but I've hit a roadblock. Here is the structure of the DB: const db = { data: [ { ...

It appears that the home page of next.js is not appearing properly in the Storybook

Currently, I am in the process of setting up my next home page in storybooks for the first time. Following a tutorial, I successfully created my next-app and initialized storybooks. Now, I am stuck at importing my homepage into storybooks. To achieve this, ...

How can the parent div adjust its height to match the tallest child div?

Currently working on a web project where I have a div named "page", housing two other divs - one on the left called "navigation" and the one on the right named "content". I am aiming to set the height of the "page" div to match the height of the tallest di ...

Unable to create account using PHP

Every time I attempt to create an account, the data I receive is always problematic. The username must be between 3 and 15 characters I find it frustrating that the account creation never goes through successfully. What baffles me even more is that af ...