Steps for creating a sleek vertical slider with transitional effects using JavaScript and HTML

Take a look at my code snippet:

<table>
  <tr>
    <td onclick="shownav()">Equations of Circle
      <div id="myDownnav" class="sidenav">
        <a>General Info</a>
        <a>In Polar Coordinates</a>
        <a>From two end-points of the diameter</a>
        <a>Passing through three points</a>
      </div>
    </td>
  </tr>
</table>

I'm trying to display the four links within the div with an ID of myDownnav when clicking on the td element containing the text Equations of Circle. These are the CSS styles I have applied:

.sidenav{
  height: 0; /*This value will be updated by JavaScript*/
  width: 100%; 
  position: relative; 
  z-index: 10;
  top: 0; 
  left: 0;
  background-color: rgba(0,0,0,0.6);
  transition: 0.5s; /*Adds a 0.5s transition effect to slide down the sidenav*/
}

/* Styling for the navigation menu links */
.sidenav a {
  padding: 10px;
  text-decoration: none;
  font-size: inherit;
  color: lightsteelblue;
  display: block;
  transition: 0.3s;
  background-color: rgba(0,0,0,0.5);
}

/* Change the color when hovering over navigation links */
.sidenav a:hover {
  color: lightsteelblue;
  background-color: rgba(0,0,0);
}

And here's my JavaScript function named shownav():

function shownav(){
  var z=document.getElementById('myDownnav').style;
    document.getElementById("myDownnav").style.height =z.height==0? '100%': 0;
}

What adjustments do I need to make? Currently, I'm encountering the following issues:

  1. All four links appear visible even before clicking on the td element.
  2. Upon clicking the td, only the background changes without revealing the links.
  3. The links remain visible and do not disappear despite setting the div height to 0 after multiple clicks.
    P.S. I am aware that using inline functions like onclick is discouraged. Any alternative suggestions are welcome.

Answer №1

.sidenav{
  max-height: 0; /* Adjusted dynamically using JavaScript */
  width: 100%; 
  overflow: hidden;
  background-color: rgba(0,0,0,0.6);
  transition: max-height 0.5s ease; /* Smooth transition effect to slide the sidenav down*/
}

.sidenav.show {
    max-height: 500px;
}
function toggleNav(){
    let nav = document.querySelector('.sidenav');
    nav.classList.toggle('show');
}

To create a height transition, it's recommended to use max-height and overflow properties. This approach is explained in this answer. It's advised to handle class toggling with JavaScript instead of mixing CSS directly within the JS code for better development and debugging process. More details can be found here.

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

A guide on converting JSON strings into arrays using Javascript

In my JavaScript program, I have a Mocha test that checks if all available currencies are displayed in a drop-down list: it('displays all available currencies in drop down list', () => { return invoiceEditPage.details.currencyDropDown.dr ...

Calculating variables in JavaScript

Explanation from Mozilla Documentation: console.log((function(...args) {}).length); // The result is 0 because the rest parameter is not counted console.log((function(a, b = 1, c) {}).length); // The result is 1 because only parameters before th ...

ASP.NET DIV is experiencing issues with Jquery functionality, causing it to flicker and remain fixed in place

As a beginner in JQuery, I am facing an issue with my code in asp.net. Whenever I click on linkbuttons, the behavior is not as expected. The div flickers and does not change its direction. My goal is to move the DIV left or right by 200px, but no matter wh ...

Is there an alternative to using CSS units of measurement when using the .css() method?

Is there a way to use .scrollTop() and .css() to adjust the margins of an element so that it floats up only when the page is scrolled, but stops at a certain margin value? I want the element to float between specific values while scrolling. The issue I am ...

Discover the correct way to locate the "_id" field, not the "id" field, within an array when using Javascript and the MEAN stack

I am working on an Angular application that connects to MongoDB. I have a data array named books, which stores information retrieved from the database. The structure of each entry in the array is as follows: {_id: "5b768f4519d48c34e466411f", name: "test", ...

What is an alternative method to retrieve form data without relying on bodyParser?

When it comes to accessing posted form data without using bodyParser, what alternatives are available? How exactly does bodyParser grant access to the data in req.body? Additionally, I am curious about the inner workings of this process on a deeper level. ...

A versatile multi-select jQuery widget featuring the ability to choose options within a specific opt

I am on the hunt for a cool jQuery tool that has the following characteristics: It enables you to create multiple groups, such as: Group 1 - Sub 1 1 - Sub 1 2 - Sub 1 3 Group 2 - Sub 2 1 Group 3 - Sub 3 1 - Sub 3 2 By clicking on Group 1, for instance, ...

Is there a framework available to animate Pseudo CSS elements?

Recently, I was working on developing a bar chart that utilized pseudo CSS elements (::before, ::after). While I successfully created bars that look visually appealing, I encountered a challenge when attempting to animate the height changes. Whenever I us ...

Steps to successfully set up a React application without encountering any installation errors

Hey everyone, I've been trying to install React on my system for the past two days but keep encountering errors. Initially, I used the commands below to install the React app and it worked smoothly: 1. npm install -g create-react-app 2. create-react- ...

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading

My jQuery ajax call between subdomains is functioning properly, but it's not sending the cookie. Is there a way to resolve this issue? ...

EJS file not displaying stylesheets and images correctly during rendering

I'm currently in the process of building a website, but when I try to render an ejs file (index.ejs), only the HTML portion is displayed without showing any stylesheets, fonts, or images. Here is the code snippet for linking the stylesheets: <!D ...

What is the best way to ensure that a specified number of list items (li) will align properly within the width of an unordered list (ul

I'm attempting to make all the li elements' width match that of my ul element. Here is the code I am using: http://jsfiddle.net/4XR5V/2/ I have looked at some similar questions on the website and tried adding: ul { width: 100%; display: tab ...

Node.js equivalent of hash_hmac

I am facing an issue while trying to sign the URL in a Node.js application similar to how it is done in my PHP app. In PHP, I use the following code to sign the URL: private static function __getHash($string) { return hash_hmac('sha1', $stri ...

File Upload yields a result of 'undefined'

Currently, I am utilizing Express File Upload. When attempting to send a post request to it, the error-handler returns an error indicating that no files were found. Upon logging console.log(req.files), it shows as undefined, causing the error to be sent ba ...

Guidelines for showcasing a map on my website with the option for users to select a specific country

Is there a method to showcase a global map on my site and let the user select a country to receive relevant information? I am aware of embedding Google Maps, however, it includes unnecessary controls like zooming which I prefer not to inconvenience the us ...

The login form is experiencing issues with submission when utilizing the submitFormHandler in ReactJS

In my single-page application, I'm working on creating a separate login page that will redirect the authenticated user to the main application. However, I'm encountering an issue where my submitFormHandler is not being invoked. The purpose of aut ...

Troubleshooting MaterialUI: Is there a way to prevent the InputLabel text from disappearing when setting a background color on the Select component?

My goal is to change the background color on dropdown elements while keeping the default text visible. Currently, if I don't explicitly set a background color on the Select component, the text specified in the InputLabel displays correctly. However, o ...

Trouble Navigating the DOM

Can you assist me in choosing the correct option c? Below is the provided HTML code: <div id="a"> <div id="b"></div> <div id="app7019261521_the_coin_9996544" style="left: 176px; top: 448px;"> <a href="d.com" oncl ...

Connection lost from JS client in Twilio's Programmable Chat

My React application utilizes the Twilio Programmable Chat library for chat functionality. The setup code typically appears as follows, enclosed within a try/catch block: this.accessManager = new AccessManager(twilioToken.token); const chatClientOptio ...

The body parser is preventing the NODE from loading on localhost

var express = require('express'); var app = express(); var bodyParser = require('body-parser'); //ISSUE LINE **app.use(parser.json);** /////////////// var todos = []; var nextTodoItem = 1; app.use(bodyParser.json); ap ...