What is the best way to toggle between display:none and display:block?

How can I delay the appearance of mobile nav items without using JS until the nav overlay fully expands for 0.4s?

In the non-overlay state, the top nav has a hamburger menu on the left and my name on the right with hidden nav links.

When in the overlay state, the hamburger menu changes to an x, the top nav covers the full page, and centralized nav links appear on the overlay.

Is it true that modern browsers will ignore animation or transition specifications if display: changes? How can I overcome this issue and delay the appearance of nav items until the overlay is fully expanded?

I found a working example here: https://codepen.io/KingKabir/pen/QyPwgG, but I'm unsure about their method?

I've attempted using visibility: and opacity: but they don't properly hide the nav elements in the non-overlay state, affecting the positioning of my name on the right side of the nav menu.

What adjustments should be made to ensure the nav elements only appear after the overlay has fully expanded?

View code snippets below:

  <nav class="nav_container" id="overlay">
    (HTML content)
  </nav>
    .cssstyles {
    /* CSS styles */
  }

Javascript snippet included for reference:

function mobileMenu() {
  // Javascript function
}

The desired outcome is for the nav_tab elements to appear after the full expansion of nav_container. However, currently, they appear prematurely before the full expansion occurs.

Answer №1

By utilizing a combination of visibility, opacity, and max-height, you can smoothly transition an element from being visible to hidden, or vice versa. This approach involves setting the max-height property to 0 when the element is hidden, and to a value like Xpx (larger than the actual size of the element) when it's visible. This technique ensures that the element does not disrupt your layout in any way, as mentioned in your query.

For illustration purposes, consider this example:

var visible = document.querySelector(".visible");

function hide() {
  visible.classList.add("hidden");
}

visible.addEventListener("click", hide);
div {
  background-color: blue;
  padding: 40px;
  color: white;
  cursor: pointer;
  transition: all .1s ease;
}

.visible {
  visibility: visible;
  opacity: 1;
  max-height: 1000px;
}

.hidden {
  visibility: hidden;
  opacity: 0;
  max-height: 0;
}
<div class="visible">Click to hide</div>

Answer №2

If you want to create a delay for your animation, simply add another parameter called 'delay' to your transition. For example:

transition: all 0.4s cubic-bezier(0.455, 0.03, 0.515, 0.955) 2s;

This will start the animation after a 2-second delay.

To help you achieve your desired outcome, I've made some CSS adjustments as outlined below:

  1. For the element .nav_container.open, replace the following lines:

    max-height: 100%;
    height: 200%;
    

    with this single line:

    min-height: 100vh;
    

    This change ensures that the container expands to at least the height of the window.

  2. For the element .nav_tab, switch from:

    display: none;
    

    to:

    display: block;
    opacity: 0;
    

    By doing this, you can hide the links and incorporate an animation for revealing them.

  3. Regarding the element .nav_tab.open, remove these lines:

    display: block;
    visibility: visible;
    

    and insert these lines instead:

    opacity: 1;
    transition: all 0.6s linear 0.4s;
    

    The duration of 0.6 seconds and a delay of 0.4 seconds ensure a smooth transition without rushing the expand effect of nav_container.

Take a look at the changes and confirm if they bring you closer to your intended design goal! ;)

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

Challenge encountered when trying to add overlay hover effect on image cards

Yesterday I faced a challenge with a tracking problem while trying to implement code on a w3schools webpage. The issue was with a hover effect triggering an overlay when the mouse was placed anywhere on the line, rather than just within the card area. I&ap ...

Using jQuery to Implement Car Turn Signals (Blinkers)

Recently, I've been faced with an unusual challenge. I need to incorporate car turning lights functionality into my website. Specifically, I have to create a scenario where clicking either the left or right button triggers the corresponding green arro ...

Center-align the text in mui's textfield

What I'm looking for is this: https://i.stack.imgur.com/ny3cy.png Here's what I ended up with: https://i.stack.imgur.com/vh7Lw.png I attempted to apply the style to my input props, but unfortunately, it didn't work. Any suggestions? Than ...

Easily style Angular directives (whether they are custom or built-in) using the native handle for CSS

In my Angular application, I have directives set up as follows: <!doctype html> <html lang="en" ng-app="cfd"> <head> ... </head> <body> <div class="container"> <!-- Header --> <div ...

Use custom styles or CSS for the file input element: <input type="file">

Can CSS be used to achieve consistent styling for <input type="file"> in all browsers? ...

Unable to set the width for a div element within the bootstrap grid layout

When trying to set the width of a div tag to 100% within a bootstrap grid system (col-lg-3), I noticed that the div ends up taking the full width of the browser instead of just 100% of its parent(col-lg-3). .sidebar { color: #fff; f ...

Adjust the location of the scrollbar without affecting the alignment of the text

Currently, I'm experiencing a design issue with the textarea tag in my React project. The layout looks like this: https://i.stack.imgur.com/JG1sm.png I am looking to shift the scrollbar to the right without altering the text direction (rtl). Utilizin ...

Methods to prevent images from spilling over into divs?

I am looking to implement a unique functionality that involves three images sliding out of the page to the right, fading in the process, and not overflowing. Simultaneously, three other images slide in to take their place. If you want to see the Framework ...

What is the best approach to dynamically bind a style property in Vue.js 3?

I am currently working with vue3 and I am trying to apply dynamic styles. This is how my vue3 template looks: <ul> <li v-for="(question, q_index) in questions" :key="q_index" v-show="question.visible" :style="{ ...

Aligning the Bootstrap navbar to the right causes the items to shift to the left

UPDATE: The issue has been resolved. It turns out that the navbar was not extending to 100% width and reaching the edges of the screen. I am currently facing a challenge with a Bootstrap 4 navbar, similar to issues I encountered with the previous version. ...

Ensure the vertical dimension of the webpage is properly maintained

I am currently working with a client who has requested a website design with a specific height for the content section. The Inquiry: Is it possible to automatically create a new page for text that exceeds the maximum height of the content section on the ...

Fixing the issue: "Tricky situation with JavaScript not working within Bootstrap 4's div tag while JS functions properly elsewhere"

Currently, I'm working on implementing a hide/show function for comments using JavaScript. Fortunately, I was able to find a helpful solution here (thanks to "PiggyPlex" for providing the solution on How can I hide/show a div when a button is clicked? ...

Enhancing PrimeNG Components with Custom CSS Styles

I have embarked on a project to develop a sleek user interface utilizing Angular 4, Angular Materials, and PrimeNG components. The most recent challenge I am facing involves the MultiSelect Component from PrimeNG: https://www.primefaces.org/primeng/#/mu ...

In Stripe.js, the background color and height of the credit card input cannot be customized

I'm struggling with customizing the appearance of a Stripe credit card input within my Vue.js application. I want to adjust the background color to #f1f1f1 and set the height to 60px, but despite trying both base styles and css, I can't seem to g ...

Obtaining the calculated background style on Firefox

Back when my userscript was only functional on Chrome, I had a setup where I could copy the entire background (which could be anything from an image to a color) from one element to another. This is how it looked: $(target).css('background', $(so ...

Tips on how to properly position the navigation bar within a wrapper element

I'm having trouble fixing the navigation bar at the top of the wrapper when scrolling down. However, when I try using position: fixed in the #nav ul, the navigation bar extends beyond the wrapper. Below is my HTML code, where the nav bar should be con ...

spinning div content in a manner reminiscent of a roulette wheel

I am looking to create a roulette game that randomly selects numbers and displays them. Initially, I attempted to use images and backgroundOffset, but it caused significant lagging issues and difficulty in checking the rolled number. Therefore, I have impl ...

Photo positioned on top of the form with Bootstrap 5

I am working on creating a responsive webpage using Bootstrap, where I need to display a large image above a form. To manage the size of the image, I have placed it inside a "peek-view" container which allows scrolling both horizontally and vertically to v ...

Hiding CheckBox items within a CheckBoxList control in ASP.NET

Currently, I am working with a CheckBoxList control that is bound to data and can potentially become very large. In order to manage this, I am implementing a filter mechanism. Here is my approach: foreach( ListItem item in this.UserRolesList.Items ) { ...

Is there a way to change the background color of a redirected page by clicking on a tag?

I have a specific goal in mind: when clicking on an anchor tag, it should redirect to page2.html and change the background color of a particular div on that page. (The anchor tag contains a URL and an ID in its href to direct to a specific section.) pa ...