What could be causing the drop-down menu to have such a jerky transition?

After trying numerous approaches, I am seeking help again. I have managed to create a dropdown menu, but now I'm wondering how to add a transition effect to it. Any suggestions on what changes I should make are greatly appreciated. Below is the code snippet:

$(document).on("click", ".menuButton", function () {
                $(".dropdown").toggleClass("visible");
             });
#menuDiv {
                display: flex;
                flex-direction: column;
                align-items: center;
                text-align: center;
              }
              
              .menuButton {
                text-align: center;
              } 
              
              .dropdown {
                display: block;
                width: 100%;
                visibility: hidden;
                transition: visibility 300ms ease-in-out;
              }
              
              .visible {
                visibility: visible;
              }
<!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Document</title>
                <script src="index.js" defer></script>
                <link rel="stylesheet" type="text/css" href="style.css" /> 
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
            </head>
            <body>
                <div id='menuDiv'>
                    <button class='menuButton'>Menu <i class="fa-solid fa-angle-down"></i></button>
                    <div id='dropdownmenu'>
                      <button class='dropdown lunch'>Lunch</button>
                      <button class='dropdown dinner'>Dinner</button>
                      <button class='dropdown locations'>Locations</button>
                    </div>
                  </div>
            </body>
            </html>

Answer №1

Utilize the .slideToggle() method from jQuery to easily handle dropdown animations. Simply set display: none on the dropdown element to control its visibility. Ensure consistency between HTML class names like dropdownmenu and jQuery targeting classes like dropdown. The container of the .dropdown elements should be targeted for toggle methods and animations.

$(".menuButton").on("click", function(e) {
  $(this).next().slideToggle("slow");
  $(this).find("i").toggleClass("fa-rotate-180");
});
:root {
  font: 2ch/1.15 "Segoe UI"
}

main {
  display: flex;
  flex-flow: column nowrap;
  align-items: center;
}

button {
  font: inherit;
  font-size: 100%;
  font-variant: small-caps;
  cursor: pointer;
}

.menuButton {
  display: flex;
  justify-content: center;
  width: 10ch;
}

dropDown {
  list-style: none;
  display: none;
  margin-left: -40px;
}

dropDown>button {
  display: list-item;
  width: 100%;
}

dropDown>button+button {
  margin-top: 8px;
}

i {
  display: block;
  margin: 0 -1ch 0 1ch;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link href="style.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
</head>

<body>
  <main>
    <button class='menuButton'>Menu <i class="fa-solid fa-angle-up"></i></button>
    <menu class="dropDown">
      <button class='lunch'>Lunch</button>
      <button class='dinner'>Dinner</button>
      <button class='locations'>Locations</button>
    </menu>
  </main>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
  <script src="index.js"></script>
</body>

</html>

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

Browser unresponsiveness triggered by excessive Ajax setInterval functionality

Here is the test code I am currently using: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Ajax - one variable test</title> <style> body{ font-size: 12px; font-family: Arial;} </styl ...

Steps for properly closing the loop to input data into an array prior to populating the JSON object in Node.js

I have encountered an issue while trying to fill all the data from my loop into an array. It seems that the loop is being executed last, causing the array to remain empty when I try to print the data. var data = new Array(); for (let station of t ...

The CSS transition will only be triggered when the class is turned on, not when it is turned off

Having some trouble with a transition effect involving multiple elements on a page. Specifically, I am working on a sliding side menu for the responsive top navigation menu. The goal is to have the relative body and a fixed header bar move to the right whe ...

Creating a countdown timer that is determined by the word count of a specific <div> element

What I have: A unique countdown timer that starts at 3 seconds and counts down to 0s. <div class="phrase"> This is a statement.</div> <p> <div style='font-family: Arial; font-size: 12px; color:gray'> <br><s ...

Show the precise selection from one dropdown menu based on the chosen value in another dropdown menu

How can I retrieve the scheduleID value from a specific MovieID value within a comboBox? Movie Selection Box: <?php $query = "select * from ref_movies ORDER BY MovieID"; $result = mysql_query($query) or die (mysql_error()); echo '<s ...

Is there a way to make Bootstrap's dark table heads hoverable without having to manually set the style?

I enjoy using hoverable tables, but I've noticed a strange behavior when including the table-dark class in my <thead>. It seems to make the header row behave like a body row, which is not what I want. Is this standard behavior and is there a way ...

React: Component styles fail to update dynamically

I have been working on creating a component that can adjust its size dynamically based on props. Currently, I am experimenting with the code below, but I've noticed that when I change the slider, the component's size doesn't actually change. ...

First keystroke in React input field registers as an empty string in log

I'm encountering an issue with my search functionality. When I enter a search term in the input field, the first value passed is always an empty string. Subsequently, for each keypress, the input seems to be offset by one item. For instance, if I type ...

In need of assistance with Ember data! Struggling to deserialize JSON into model

Here is the technology stack I'm currently using: Ember 1.10.0 Ember Data 1.0.0-beta.15 Within my application, I have defined a model as shown below: //models//acceptedtask.js import DS from "ember-data"; export default DS.Model.extend({ userAg ...

Tips for combining text and an unordered list on the same line:

I attempted to align them in a single line, but they are not cooperating. The text floating to the left looks fine, however, the list that floats to the right is causing issues. I tried various methods to make them display inline, but they still appear sli ...

evt.target consistently returns the initial input within the list of inputs

My React file uploader allows users to attach multiple file attachments. Each time a user clicks on an input, I retrieve the data-index to identify the input position. renderFileUploader() { let file_attachment = this.state.file_attachment.map(fun ...

The method provided by the FullScreen API is not effective in cancelling the fullscreen mode

After testing my code in Google Chrome, I encountered the following error message: Uncaught TypeError: Object #<HTMLDivElement> has no method 'webkitCancelFullScreen' Interestingly, I also received a similar error while running the code i ...

Why does using rgba color with an alpha value cause my div to become see-through?

I am currently attempting to assign a background color to a specific style of pop-up, but whenever I use a value that includes an alpha channel, it ends up being transparent. This is the CSS code I have been using: --color: 255, 144, 106, 0.18; background ...

Difficulty activating JavaScript function - unsure of proper implementation

I'm having trouble with calling a function instead of an alert in my code. I tried using FunctionsName(); and removing the alert(''); but it doesn't seem to be working for me :( Could someone please take a look at the following code an ...

The mouse pointer position in THREE.js does not align with the arrow tip during events

I am currently grappling with figuring out how to accurately determine the position of click events on the screen while using THREE.js alongside an Orthographic camera. Unfortunately, the position that I have managed to calculate (similarly to what is des ...

Upon sending a POST request to http://localhost:5000/getData, a 404 error (Not Found) was encountered while using axios

As a newcomer to react and node.js, I have set up a fake server running on port 5000 with an API (http://localhost:5000/getData) that contains a hardcoded array of objects. My goal is to add a new object to this API from my react frontend running on port 3 ...

Disabling the scrollbar in Selenium screenshots

When using Chromedriver to capture screenshots of webpages, my code effectively does the job. However, I am now facing an issue with removing the unsightly scrollbars from the image. Is it feasible to inject CSS into the webpage in order to achieve this? W ...

Gradually shifting alignment of Bootstrap tooltips

I am encountering an issue with Bootstrap tooltips They seem to be progressively off-center Below is the code I am using for the icons {{#each guilds}} <div class="col-sm-1"> <a href="/dash/{{this.id}}"> <div class="gicon"> ...

Conceal all video containers once a single one is chosen

Having recently delved into Javascript, I am encountering some difficulties in crafting the correct code for a specific function. The scenario involves multiple div elements, each containing 2 child div elements that switch places upon being clicked. Con ...

Removing data from every page of a table with the Datatable plugin

Currently, I am facing an issue with a table of data that I am handling using the Datatable plugin. My goal is to clear the table upon clicking a button and then redraw it with updated values received via an ajax call. The problem arises when trying to del ...