Steps to incorporate a fade or slide animation into a dropdown submenu's ul element

I'm having trouble implementing a jQuery effect or CSS fade/slide on my sub menu. The ul sub nav is initially set to display none in my CSS. When you click on the 'a' tag with the class dropdown-toggle, it adds the class 'display-sub-menu' to its child ul (which then changes to display block according to my CSS). Here's my code:

HTML:

<ul class="nav-list">
    <li><a href="index.html">Home</a></li>
    <li>
        <a class="dropdown-toggle" title="About Us">About Us</a>
        <ul>
            <li><a href="about-us.html">About Our Business</a></li>
            <li><a href="meet-the-team.html">Meet The Team</a></li>
        </ul>
    </li>
</ul>

This is my current jQuery script:

$('a.dropdown-toggle').click(function(e) {
    $(this).next().toggleClass('display-sub-menu', 250);
}); 

Currently, this only toggles between hiding and displaying the sub menu. I would like to add a slide effect using slideToggle but can't seem to get it to work.

Answer №1

Instead of using a toggle class, you have the option to use .fadeToggle()/.slideToggle() for a fade or slide effect

Both methods allow you to specify a duration as an argument, such as passing 250:

$('a.dropdown-toggle').click(function (e) {
    $(this).next().fadeToggle(250);
});

Alternatively,

$('a.dropdown-toggle').click(function (e) {
    $(this).next().slideToggle(250);
});

See Demo in Action

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

The function 'toLowerCase' cannot be found for the type 'string | number | string[]'. Similarly, the function 'toLowerCase' cannot be found for the type 'number'

Currently, I am working on a Laravel project using Laravel Mix. I am attempting to create a table with filter functionality. However, when I insert the following code into my TS file: import $ from 'jquery'; import 'bootstrap'; $(() = ...

Tailwind CSS Sturdy Top Navigation Bar

I have been attempting to implement a Fixed Navigation Bar using Tailwind CSS and have the main page scroll with a sticky effect, but I'm encountering difficulties in getting it to function properly... Here is the current state of my progress: https ...

What is the best way to rate a particular image?

while ($row = mysqli_fetch_array($result)) { echo ""; echo "<div id='img_div'>"; echo "<i class='fas fa-times'></i>"; echo "<img class='photoDeGallery' s ...

Regularly check in with the server via ajax calls

I have a page that needs to send periodic "background" ajax requests after it is loaded. These requests should be sent at specific time intervals. Would using cron be the best option for this task, considering that I have never used it before? Are there a ...

Passing Value in Bootstrap 4 Modal

I'm having trouble figuring out how to pass data to a modal. Here is an example of what I am trying to achieve: $('#exampleModal').on('shown.bs.modal', function () { var userID = $(this).data('userId'); $('# ...

What could be causing the malfunction in this multi-select code developed by loudev?

I tried adding this link: After following the instructions, the code is still not functioning correctly. I made sure to include jQuery scripts as well because I thought they might be necessary. I'm having trouble identifying where the mistake lies. ...

Empty space under the banner in Wordpress theme Avada

I can't seem to locate the CSS class for a blank space that appears below the header on one of my pages. This space is situated between the header and "SERVICIOS 24 HORAS". Any ideas on how I can remove this mysterious gap? My website is built wit ...

Troubleshooting a jQuery window scrolling problem alongside Locomotive Scroll on a single webpage

I am in the process of developing a website and using the Locomotive Scroll plugin to ensure a smooth scrolling experience. The plugin is functioning well, but I am facing an issue with implementing a fixed header (top menu) that has the usual reduced heig ...

Using Ajax to implement the Modal IF functionality

Currently in the process of registering a domain, utilizing two modals in the process. Through Ajax, I have set it up so that if the response is 1, an alert stating that the domain is available is displayed. If the response is 0, an alert stating that the ...

Uniting the client-side jQuery and server-side Express for enhanced functionality

Currently, I am deepening my understanding of Express/Node. My goal is to construct a basic HTML form that undergoes client-side validation (using jQuery/AJAX) while also being processed server-side (via Express.js). The application's complexity remai ...

Width of flex container overflowing from relative position is at its full capacity

I'm attempting to create a horizontal line across an overflowing flex container by using an absolutely positioned child element with both the left and right set to 0. This requires the container to have a relative position to contain the absolutely po ...

Ul list centered alignment appears misaligned to the left in Safari browser exclusively

I'm experiencing an issue with my menu alignment. It appears perfectly centered in FF, Chrome, and IE, but shifts to the left in the Safari browser. I'm not sure if there is a specific CSS property I need to use to correct this alignment. <d ...

Generate a div element dynamically when an option is selected using AngularJS

I'm having trouble dynamically creating div elements based on the selected option value, but for some reason ng-repeat isn't working as expected. Can you help me figure out what I'm missing? Here's the HTML snippet I'm using - &l ...

Launch a bootstrap modal using jQuery, showcasing a designated HIDDEN section (excluding nav-tabs)

This unique modal box does not have a visible tab. Instead, it utilizes the href attribute for navigating to other pages. <div id="bootmodal" class="modal fade" tabindex="-1" data-width="370" data-backdrop="static" data-keyboard="false" style="display: ...

When I open my website in the browser, the bootstrap card-deck design is not being displayed correctly

I encountered a strange issue while designing my website. The code I wrote for the Bootstrap card-deck class in codeply.com was working perfectly fine. However, when I copied the same code into my PyCharm code editor and then pasted the file link into my b ...

Why might the MIME type 'application/json' be refused when the dataType is JSON?

Is it true that if the dataType is set to JSON in jQuery, it expects a JSON response? I found http://api.jquery.com/jquery.ajax/ to be informative on this topic. In the following function, I have experimented with specifying both no dataType (for jQuery&a ...

Tips for Resizing and Reviving Divs with jQuery

I have recently ventured into the world of web development to assist a family member with their website. My knowledge and experience are limited, but I am facing an interesting challenge. I am trying to manipulate certain divs as users scroll down the page ...

Dynamically adjust the height of a parent div to match its child div's height upon clicking using JavaScript

Within my div element (divChatContainer), there is a top div (divChatContainerTop) containing a "span" element. When this "span" element is clicked, I want the container div to resize to the height of the "divChatContainerTop" (essentially minimizing the c ...

delete the initial background color assigned to the button

The "ADD TO CART" and "save design" buttons are currently displaying as shown in the image below. I would like to make the "SAVE DESIGN" button look similar to the "ADD TO CART" button, meaning I want to remove the background-color and dot symbol. Code ...

Tips for adjusting webpage layout automatically based on window dimensions?

As someone who is new to web development, I am in search of a simple solution that allows my developing page to dynamically resize according to the window size without disrupting the layout. Additionally, I am trying to center the page, but experiencing is ...