The slideToggle() function appears to be malfunctioning when sliding up or down, as it

Currently, I am in the process of creating a dropdown menu. However, I am facing an issue where the menu does not slide down or up smoothly when clicked. The animations seem to be delayed or non-existent.

$('.menu-vertical .nav-menu > li').on('click', function(e) {
  e.preventDefault();
  $(this).find('ul').slideToggle('2000', 'swing');
});
.menu-vertical .nav-menu li ul {
  position: relative;
  background: #f5f5f5 !important;
  top: 0;
  display: none;
  transition: all .4s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav class="menu-vertical">
  <ul class="nav-menu">
    <li>this is head
      <ul>
        <li>this is one</li>
        <li>this is one</li>
        <li>this is one</li>
        <li>this is one</li>
      </ul>
    </li>
    <li>head is head</li>
    <li>head is head</li>
  </ul>
</nav>

Answer №1

It seems like there might be an issue with conflicting transitions in your CSS.

Additionally, make sure that the timing value for the slide is a number and not a string. Using a string will default to 400ms instead of the specified 2000ms.

$('.menu-vertical .nav-menu > li').on('click', function(e) {
  e.preventDefault();
  $(this).find('ul').slideToggle(2000, 'swing'); // change '2000' to 2000
});
.menu-vertical .nav-menu li ul {
  position: relative;
  background: #f5f5f5 !important;
  top: 0;
  display: none;
  /*transition: all .4s ease; <- remove this bit*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<nav class="menu-vertical">
  <ul class="nav-menu">
    <li>this is head
      <ul>
        <li>this is one</li>
        <li>this is one</li>
        <li>this is one</li>
        <li>this is one</li>
      </ul>
    </li>
    <li>head is head</li>
    <li>head is head</li>
  </ul>
</nav>

Answer №2

The issue stems from the absence of the crucial $(document).ready() event handler.

$(document).ready(function() { //forgot this very important
  $('.menu-vertical .nav-menu > li').click(function(e) {
    e.preventDefault();
    $(this).find('ul').slideToggle(2000, 'swing');
  });
});
.menu-vertical .nav-menu li ul {
  position: relative;
  background: #f5f5f5 !important;
  top: 0;
  display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <title>Document</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js></script>
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css>
</head>

<body>
  <nav class="menu-vertical>
    <ul class="nav-menu>
      <li>this is head
        <ul>
          <li>this is one</li>
          <li>this is one</li>
          <li>this is one</li>
          <li>this is one</li>
        </ul>
      </li>
      <li>head is head</li>
      <li>head is head</li>
    </ul>
  </nav>
</body>

</html>

Answer №3

menu-vertical .nav-menu li ul {
  position: relative;
  background: #f5f5f5 !important;
  top: 0;
  display: none;
  transition: **background** .1s ease-in;
}
I have updated the CSS code for your solution to only include specific properties for transition effects.

Instead of using "all" in the transition property, it's better to specify individual properties that you want to target for transitions.

Remember: The first parameter in the transition property should be a specific CSS property you want to animate.

For more information on the transition property, you can refer to this link:

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

Tips for embedding external URLs into a div element without using an iframe

I need help loading an external URL into a div without using iframe, embed, or object tags. I have tried examples but they do not seem to be working properly. Here is an example of what I have tried: $("#testDiv").load("//localhost:8000/cities/Mountain%2 ...

JavaScript code for extracting the value of a specific table cell from the provided screenshot

Looking at the image below, as someone new to JavaScript development, I have two questions. First, how can I directly retrieve the value of the second td from $('#cart-subtotal-order.total.subtotal td') in JavaScript code? Secondly, I need to kno ...

Having trouble with Vue 3 Composition API's Provide/Inject feature in Single File Components?

I am currently developing a VueJS 3 library using the Composition API. I have implemented Provide/Inject as outlined in the documentation. However, I am encountering an issue where the property in the child component remains undefined, leading to the follo ...

Enhance HTML Performance with Resource Preloading

Just a quick query I wanted to throw out there - would prefetching resources like js scripts and images actually slow down the page load time? For instance, let's say I have a page with several links such as: <link rel="prefetch" href="http://exa ...

The function nested within a constructor that includes `this.route` is not assigning it as an array

this.routeHandler = function () { let stations = ['KSFM', 'KPWM'] let coordinates = []; let allCoords = []; if (Array.isArray(stations) == true) { for (let i = 0; i < fetchRoute().length; i++) { fo ...

The user_login view in the account views did not provide an HttpResponse object, but instead returned None

Error: Unable to register or log in - account.views not returning HTTP response # accounts/views.py from django.shortcuts import render, redirect from django.contrib.auth import login, authenticate, logout from account.forms import RegistrationForm, Use ...

Identifying touchscreen capability through media queries

Is there a secure method, utilizing media queries, to trigger an action when not using a touchscreen device? If not, would you recommend opting for a JavaScript solution like !window.Touch or Modernizr? ...

Is there a way to bring to life the addClass() and removeClass() jQuery functions through animation?

I am currently developing a website and I want to be able to toggle the visibility of sections by using the ".hidden" bootstrap class within click events. Here is the basic code snippet: $('selector').click(function(){ $('*part-to-hi ...

Unable to open modal using a button in PHP

<?php $result = mysqli_query($con, $sql); while ($row = mysqli_fetch_array($result)) { echo '<tr><td>'; echo '<h5 style="margin-left:6px; color:black;">' . $row['id'] . '</h5> ...

Two sets of buttons, however, one set is carrying out the incorrect function

I am trying to create two button sets that, when clicked, are supposed to angle the text below them at 45 degrees. However, I am facing an issue where both set 1 and set 2 only control the text linked to button 1. I need each button set to control their ...

Automated pagination in Jquery running seamlessly

I have successfully created pagination using jQuery. Although the script is functioning properly, I now want it to automatically switch between different pages: <script> $(document).ready(function(){ $("#article_load_favourites").load("indexer_favo ...

Animate the CSS position from its current location to the bottom, set at 0%

I am trying to create a CSS animation to slide up an image that is sticking out of the bottom of the screen. The challenge is that the dimensions of the image are variable, so I don't know how much of it is initially visible outside the screen. What ...

How can I modify the appearance of the bootstrap scrollbar with scrollspy?

I'm struggling to modify the color of the scrollbar in Bootstrap scrollspy despite having a functional scrollbar. Here is an example of my code: HTML: <body> <div class="container, scrollbar" id="myScrollspy"> <div class=" ...

Code in jQuery or JavaScript to retrieve precise node information for the currently selected form field, text, or image on a webpage

Looking to retrieve specific information about the item that has been clicked on a web page using jquery. The clickable item could be a form element (like a checkbox, text box, or text area) or a section of text within a paragraph, div, list, or image... ...

Arranging numbers in JavaScript lists

empList = [ { "Account": "AAA - 0029", "Available": "$100" }, { "Account": "BBB- 0146", "Available": "200" }, { "Account": "AAA - 1812", "Available": "300"}, { "Account": "CCC- 2019", "Available": "400"}, { "Account" ...

"Utilizing Google Charts DataTable to easily adjust column number based on data dynamics

Struggling with managing JavaScript objects... Currently, I am working on generating a Google Chart based on user selections. The process involves two multi-select lists that construct an object as shown below: $('option:selected', $('#slC ...

Four divs containing texts of varying sizes

I have varying sizes of text that I would like to organize into 4 divs in the center of the page, similar to this image http://firepic.org/images/2015-08/22/0b0r536o40es.png However, it seems that my pink div is causing the ones below it to be pushed down ...

The functionality of Google's AdMob in the context of Meteor.js/Cordova is currently not defined

I have been working on developing a mobile app specifically for android platforms using Meteor.js As I am nearing completion, I decided to integrate Google AdMob into my application. Unfortunately, despite my efforts, I could not find any suitable package ...

Exploring depths of data with AngularJS: multiple levels of drilling

I am new to AngularJs and facing a challenge with a complex JSON structure that I need to use for an auto complete feature. I want to create an auto complete for only the child elements within the structure, without displaying the parent categories. Acce ...

Is it possible to reuse a variable within a single HTML tag when using Angular 2?

I encountered a strange issue with Angular 2 that may be a bug. I noticed that I couldn't print the same variable in a template twice within the same HTML tag. When I tried to use the following code, it resulted in error messages. <div class=" ...