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

Modify the color of the matSnackbar

I'm having trouble changing the snackbar color in Angular using Angular Material. I tried using panelClass in the ts file and adding it to the global css, but the color remains unchanged. Any suggestions on how to resolve this? I am still new to this ...

Vue.js combined with Video.js (MPEG-DASH) is throwing an error: (CODE:4 MEDIA_ERR_SRC_NOT_SUPPORTED)

I am facing an issue with Video.js when using it as a component in vue.js. I receive a .mpd link from a server and I want to display the video from that link. Following the example in the documentation of Video.js and Vue integration. Every time I call th ...

The Bootstrap modal backdrop is now displaying prominently in front of the modal following the latest Chrome update

Chrome version: Version 111.0.5563.64 (Official Build) (x86_64) Bootstrap: 4.3.1 Recently, there has been an issue with the modal backdrop appearing in front of the modal itself after a Chrome update. The problem does not seem to be related to z-index, an ...

having trouble with displaying the results on the webpage

I am facing an issue while trying to display the data fetched from the database: {"results": ["USA", "Canada"]} {"message":"Could not find any countries."} //else An error was encountered in the console: Uncaught TypeError: Cannot read property 'l ...

What strategies can be implemented to decrease the value of the writing box by 2.5%?

I would like to decrease the value in the input box by 2.5%. Here is the HTML code snippet: <div class="ZakatCalc"> <div class="calcimg"> <img src="" alt="" srcset="" class=" ...

Trimming text down to a specified index

I am facing a challenge where I need to clip a String at a specific index, taking into consideration that the String may contain HTML tags. The goal is to skip over any HTML tags while clipping the text. For instance, if the initial String is: "Les pirat ...

Using jQuery, wrap two specific HTML elements together

I am working with a set of elements: <div id="one">ONE</div> <div id="two">TWO</div> <div id="three">THREE</div> <div id="four">FOUR</div> <div id="five">FIVE</div> My goal is to wrap a DIV tag ...

Is the Material-UI 1.3 font size failing to adapt to varying screen widths?

Looking to create an app with responsive font sizes using material-ui (v1.3)? Also, want the font size to shrink when the screen size is smaller and adjust automatically. However, the current code doesn't update the font size dynamically as the screen ...

GitHub jQuery Page

Unable to display images based on user choice from the dropdown box, which is causing an issue after committing to GitHub website. There are no errors shown in the developer tool, but when hovering over the image link it says 'Could not load the imag ...

I am struggling to retrieve the temporary name of an image upload with PHP and AJAX

I am working on a project where I need to extract the temporary name of an uploaded image using PHP and Ajax. While I have successfully retrieved the file name of the uploaded image, I am facing difficulties in getting the temporary name. Below is the snip ...

JavaScript: Increasing the date by a certain number of days

I've been researching various topics and so far, I haven't come across one that addresses my specific issue. Here's the task at hand: 1) Extract a bill date in the mm/dd/yy format, which is often not today's date. 2) Add a dynamic ...

Bootstrap Tags Input is unable to function properly with data stored locally

I am currently working on developing a Project Manager tool that allows for the addition of multiple individuals to a single project. To accomplish this, I decided to incorporate the use of Bootstrap Tags Input by following the examples provided for Typeah ...

Having difficulty linking a click event to an Anchor tag within a React component

Here is the code snippet for ComponentA.js: This is the return statement inside the component return ( ...... <a id="MytoolTip" ...... <ComponentB content={ ` <div class="share_cart_tt ...

You can definitely invoke a function within a React hook

This code snippet showcases a component utilizing Hooks in React Native import React, { useEffect, useState } from 'react'; import { StyleSheet, Text, View, TouchableOpacity, Animated } from 'react-native'; import CAStyles fro ...

The functionality of linear mode seems to be malfunctioning when using separate components in the mat-horizontal-stepper

In an effort to break down the components of each step in mat-horizontal-stepper, I have included the following HTML code. <mat-horizontal-stepper [linear]="true" #stepper> <mat-step [stepControl]="selectAdvType"> <ng-template matStep ...

What is the best location to specify Access-Control-Allow-Origin or Origin in JavaScript?

After researching, I found out that I need to set my Access-Control-Allow-Origin or Origin tags. But the question is: where do I actually add these? The suggestions were to use them in the header section. I couldn't find any detailed explanation on t ...

The ng-switch functionality does not seem to be functioning properly, even when the is

My ng-switch is displaying both buttons instead of just one when isActive is false. Can anyone help me figure out what I am doing wrong? <div ng-switch="user.IsActive"> <div ng-switch-when="false"> <button type="button" (click)="activ ...

There seems to be a glitch in the angularjs application

This is my code for the Services.js file: angular.module('RateRequestApp.services', []). factory('rateRequestAPIservice', function($http) { var rateRequestApi = {}; rateRequestApi.getData = function () { retur ...

How can I exclusively enable my own Java Scripts while disabling all others using Grease Monkey?

Help needed with getting a Grease Monkey script using JQuery to work on a malfunctioning site. I'm attempting to run the GM script below, but encountering a JavaScript error on the target page preventing my code from executing. // ==UserScript== // ...

Just built a React App including a blog functionality, but for some reason the blog posts are not showing up. Any suggestions on how to fix this issue

I'm currently working on a project that involves creating a blog page for our react app. Despite my efforts, I am facing difficulty in getting the blog posts to show up. In order to retrieve all the posts from my MYSQL database, I have set up an API ...