How to make ul button text fade in and out when button is clicked?

Omar Dajani

Quick question, how can I make my Ul buttons slowly appear (when menu button is clicked) using Javascript. Here is what I have so far:

Basically, when the menu expand nav function is called I want the li tags to slowly appear/disappear. How can I go about this? This is only a snippet of my script because it is quite a lot.

Thanks!

setup();

function setup() {
  document.getElementById("extend-nav-container").style.display = "none";
}

function expandNav() {
  if (document.getElementsByTagName("body")[0].style.overflow === "hidden") {
    document.getElementById("extend-nav").style.height = "0px";
    document.getElementsByTagName("body")[0].style.overflow = "";
    document.getElementById("extend-nav-container").style.display = "none";
  } else {
    document.getElementById("extend-nav").style.height = "100vh";
    document.getElementsByTagName("body")[0].style.overflow = "hidden";
    document.getElementById("extend-nav-container").style.display = "";
    document.getElementById('fadeout').style.opacity = '0';
  }
}

function myFunction(x) {
  document.getElementById("extend-nav").style.height = "0px";
  document.getElementsByTagName("body")[0].style.overflow = "";
  document.getElementById("extend-nav-container").style.display = "none";
}

var x = window.matchMedia("(max-width: 768px)")
myFunction(x);
x.addListener(myFunction);
.new-nav {
  background: black;
  display: flex;
  transition: 0.5s;
  justify-content: center;
}

.new-nav .container {
  width: 98%;
  height: 98%;
  transition: 0.5s;
}

#extend-nav ul {
  list-style-type: none;
  height: 100%;
  transition: 1s;
}

#extend-nav li {
  margin-bottom: 10px;
}

#extend-nav a {
  text-decoration: none;
  color: white;
  font-weight: lighter;
  transition: 0.5s;
}
<div class="new-nav" id="extend-nav">
  <div class="container" id="extend-nav-container">
    <ul id="second-nav-ul">
      <li><a href="#">Home</a></li>
      <li><a href="#">Ban</a></li>
      <li><a href="#">Warn</a></li>
      <li><a href="#">Gift</a></li>
      <li><a href="#">Records</a></li>
      <li><a href="#">Moderator</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Edit</a></li>
      <li><a href="#">Account</i></a></li>
      <li><a href="#">Sign Out</a></li>
    </ul>
  </div>
</div>

Toan Lu

You can use javascript to toggle a class when user click on your hamburger or somewhere on the menu.

Display: none cannot be applied with transition effect, so you need to use visibility attribute instead.

function myFunction() {
    var element = document.getElementById("second-nav-ul");
    element.classList.toggle("collapse");
}
.nav {
  list-style-type: none;
  margin: 0;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #000;  
  height: 50px;
}

ul li a {
  color: #FFF;
}

.collapse {
  height: 0px;
  padding: 0px;
  visibility: hidden;
  transition:all 0.1s linear;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="new-nav" id="extend-nav">
  <button onclick="myFunction()">Toggle Me</button>
  <div class="container" id="extend-nav-container">
    <ul id="second-nav-ul" class="nav">
      <li><a href="#">Home</a></li>
      <li><a href="#">Ban</a></li>
      <li><a href="#">Warn</a></li>
      <li><a href="#">Gift</a></li>
      <li><a href="#">Records</a></li>
      <li><a href="#">Moderator</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Edit</a></li>
      <li><a href="#">Account</a></li>
      <li><a href="#">Sign Out</a></li>
    </ul>
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I fade a div out when a button is clicked?

Javascript: How to make fade in and out transition when button is pressed?

Fade Button in and Out While being clicked/not clicked

How to create a fade out button

How to smooth fade in-out for toggle text using button click?

how to set a text as a label when button is clicked?

how to disable a button and change text when clicked

How to change text in button when clicked?

How to make text fade out

How to make other button functions automatically run when a button is clicked

How to make a button that creates a button when clicked (in HTML and JavaScript)?

Making text fade in/out on button click in React

text that pops up when a button is clicked then fades out html

In ADF Mobile, How to change the button text when the button is clicked?

How can I make a div slowly slide out (with CSS' transition property) when a specific button is clicked?

How can I make a new scene fade in when a button is pressed?

How to make a text field into input field when a button/icon is clicked in Angular/Typescript?

How can I make my button change the text it has when clicked?

Fade Out an Image when click on button, change image url and then fade in. How to do it?

How can I make a search button that can combine my site url + the text on the input, and when the button was clicked, it will go to that link?

How to make a button change color when clicked and then change back to its original color when a different button is clicked?

How to fade out just one button with Jquery

jquery: how to change text on button when click and when is clicked

How to make a button disabled when no text is in a textbox?

How to fade in and fade out back and forth in android studio without button

How to fade in / fade out a button on scrolling down / up respectively

how to make items true in an object when user clicked on button vuejs?

How to make a circular ripple on a button when it's being clicked?

How to make a JFrame show advanced options when a button is clicked