Unwanted slideToggle behavior

I am currently working on creating a navigation menu using jQuery's slideToggle function. However, I have encountered an issue where the dropdown menu keeps sliding up and down quickly when hovering over all of them at once.

I would like to modify these transitions so that when transitioning from a list with a larger height to a lower height, the dropdown menu slides directly to the boundary of the lower height menu instead of sliding up to the top first and then back down to the lower height.

For example, let's say we have Tab A(>Dropdown A) & Tab B(>Dropdown B). Dropdown A has a height of 150px and Dropdown B has a height of 100px. What I want is: When hovering over Tab A, Dropdown A slides down to 150px from the top. Then, if I hover over Tab B, Dropdown A disappears and Dropdown B is shown. The transition should slide from 150px to 100px instead of going from 150px > 0px > 100px.

Would it be possible to achieve this effect simply with jQuery? Are there any restrictions or limitations that may prevent me from achieving my desired result? Any insights or assistance would be greatly appreciated! Thank you!

 $(function(){
            $('.dropdown').hover(function(){
                $(this).find('div').slideToggle();
                $('.dropdown-content').css("left","0");

            })    
        })
            body{
              margin:0;
            }
            ul {
                list-style-type: none;
                margin: 0;
                padding: 0;
                overflow: hidden;
                background-color: blue;
            }

            li {
                float: left;
            }

            li a, .dropbtn {
                display: inline-block;
                color: white;
                text-align: center;
                padding: 14px 16px;
                text-decoration: none;
            }

            li a:hover, .dropdown:hover .dropbtn {
                background-color: white;
                color:blue;
            }

            li.dropdown {
                display: inline-block;
            }

            .dropdown-content {
                display: none;
                position: absolute;
                background-color: white;
                width:100%;
                box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
                z-index: 1;.
                left: 0 !important;
            }

            .dropdown-content a {
                color: black;
                padding: 12px 16px;
                text-decoration: none;
                display: block;
                text-align: left;
            }

            .dropdown-content a:hover 
                background-color: #f1f1f1;
            }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
            <li class="dropdown">
                <a href="javascript:void(0)" class="dropbtn">Home</a>
                <div class="dropdown-content">
                    <a href="#">Link A</a>
                    <a href="#">Link B</a>
                    <a href="#">Link C</a>
                    <a href="#">Link D</a>
                    <a href="#">Link E</a>
                    <a href="#">Link F</a>
                    <a href="#">Link G</a>
                </div>
            </li>
            <li class="dropdown">
                <a href="javascript:void(0)" class="dropbtn">News</a>
                <div class="dropdown-content">
                    <a href="#">Link I</a>
                    <a href="#">Link II</a>
                    <a href="#">Link III</a>
                </div>
            </li>
            <li class="dropdown">
                <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
                <div class="dropdown-content">
                    <a href="#">Link 1</a>
                    <a href="#">Link 2</a>
                    <a href="#">Link 3</a>
                    <a href="#">Link 4</a>
                    <a href="#">Link 5</a>
                </div>
            </li>
        </ul>

        <h3>Test</h3>
        <p>Testing</p>

Answer №1

Make sure to include the .stop() method:

$(function(){
  $('.dropdown').hover(function(){
    $(this).find('div').stop().slideToggle();
    $('.dropdown-content').css("left","0");
  });
});
body {
  margin:0;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: blue;
}

li {
  float: left;
}

li a, .dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
  background-color: white;
  color: blue;
}

li.dropdown {
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: white;
  width: 100%;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;.
  left: 0 !important;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #f1f1f1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
            <li class="dropdown">
                <a href="javascript:void(0)" class="dropbtn">Home</a>
                <div class="dropdown-content">
                    <a href="#">Link A</a>
                    <a href="#">Link B</a>
                    <a href="#">Link C</a>
                    <a href="#">Link D</a>
                    <a href="#">Link E</a>
                    <a href="#">Link F</a>
                    <a href="#">Link G</a>
                </div>
            </li>
            <li class="dropdown">
                <a href="javascript:void(0)" class="dropbtn">News</a>
                <div class="dropdown-content">
                    <a href="#">Link I</a>
                    <a href="#">Link II</a>
                    <a href="#">Link III</a>
                </div>
            </li>
            <li class="dropdown">
                <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
                <div class="dropdown-content">
                    <a href="#">Link 1</a>
                    <a href="#">Link 2</a>
                    <a href="#">Link 3</a>
                    <a href="#">Link 4</a>
                    <a href="#">Link 5</a>
                </div>
            </li>
        </ul>

        <h3>Test</h3>
        <p>Testing</p>

Learn more about .stop()

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

Is there a way to eliminate the bottom border on the active navigation tab?

Here's the information I've gathered: Below is a snippet of code that I have: .nav-tabs { border-bottom: 3px solid #3FA2F7 !important; } .nav-tabs .nav-link { color: #02194A; font-size: 13px; padding: 12.5px !important; } ...

How can JavaScript/jQuery be used to update LocalStorage Objects when editing a form?

Having trouble pinpointing an issue with my code. Despite making modifications, the values in localStorage are not updating as expected. Any suggestions on what may be causing this problem? Note: Changing const idx to const i resulted in only the final va ...

Dynamically generating fields in JavaScript causes the fields to mysteriously vanish

Below is the JavaScript code I am working with: <script language="javascript"> function addInput() { document.getElementById('text').innerHTML += "<input type='text' value='' name='a1[]' size='60&a ...

I'm struggling to locate the space that should be between these elements

After accidentally starting in quirks mode without declaring a doctype, I realized my mistake and corrected it by declaring a doctype. However, there is a persistent issue with a space between .car-box-img and car-box that I can't seem to locate in t ...

Using JQuery's onChange event triggers actions based on the current values of input fields. However, in some

I am currently tackling an issue with a module that is designed to automatically select the only valid value from a Multi- or Single selection field when there is just one valid option and an empty choice available. Initially, everything was working smoot ...

Creating anchor links with #id that function correctly in an Angular project can sometimes be challenging

My backend markdown compiler generates the HTML content, and I need Angular to retrieve data from the server, dynamically render the content, and display it. An example of the mock markdown content is: <h1 id="test1">Test 1<a href="#test1" title ...

The particular division is failing to display in its designated location

This is quite an interesting predicament I am facing! Currently, I am in the process of coding a website and incorporating the three.js flocking birds animation on a specific div labeled 'canvas-div'. My intention is to have this animation displa ...

Iterating through an array with ngFor to display each item based on its index

I'm working with an ngFor loop that iterates through a list of objects known as configs and displays data for each object. In addition to the configs list, I have an array in my TypeScript file that I want to include in the display. This array always ...

Executing the fadeIn() callback in JQuery before the animation finishes when invoking a function

I've noticed quite a few posts discussing this issue on the site, but most of them focus on multiple animations. After trying some solutions, I have narrowed down my problem to one specific box that I'm attempting to fade. Strangely enough, when ...

Ensure to preselect the radio button based on the Day's value

I have set up my Radio buttons with corresponding content to be displayed when clicked. I want to automatically pre-select the tab button based on the current day. For example, if today is Sunday, the page should load showing the contents for Sunday. If i ...

Steps to generate a script that tracks the number of visible divs and dynamically updates when a user applies a filter

I'm facing challenges in developing a script that can count and display the number of divs selected by the user compared to the total number of available divs. If you'd like to see the whole problem, check out this JSFiddle: http://jsfiddle.net/ ...

Guide to implementing an AJAX loader during page load in PHP

Can anyone assist me in creating a page loader for when the page is loading? I am working on implementing a PayPal payment system. I have noticed that some pages display a loader (ajax loader) while directing to the PayPal gateway. I also want to add an ...

Is it possible to activate events on select tags even when they have the disabled attribute?

All the select options on this page have been disabled with an ID that ends in _test, and everything seems to be functioning properly. However, I would like to display a title when the selects are disabled and the mouse hovers over them. The issue arises ...

Incorporate attributes into object properties in a dynamic manner

Currently, I am experimenting with bootstrap-multiselect and my goal is to incorporate data attributes into the dataprovider method. Existing Data Structure: var options = [ {label: 'Option 1', title: 'Option 1', value: ' ...

THREE.js: dual scenes sharing identical camera positions

I'm currently working on a project where I have two overlapping scenes. The top scene can be faded in and out using a slider, and users can rotate objects within the scene for a better view. My challenge is to keep the camera position consistent betw ...

Having trouble retrieving data from the controller in JSON format

I am having trouble getting the data from the controller to the view in JSON format, and I can't figure out why it's not working. This is my controller (I request it on the index action): public class HomeController : Controller { ...

What is the best way to use a button to hide specific divs upon clicking?

Is there a way to use a button onclick event to hide specific divs within a parent div? I've tried using .toggleClass('.AddCSSClassHere') but I'm not sure how to apply it to multiple divs. The jQuery snippet provided only allows me to h ...

The default padding and margin in Bootstrap are making the images protrude beyond the edges of the container

I have an issue where my images are stretching into the padding or margin that bootstrap uses to separate columns. I want to maintain the column division but avoid having the images stretch. While I have managed to shrink the image using padding, the white ...

What strategies can be utilized to raise the max-height from the bottom to the top?

I am facing the following coding challenge: <div id = "parent"> <div id = "button"></div> </div> There is also a dynamically generated <div id="list"></div> I have successfully implem ...

"Troubleshooting: Spring Boot application fails to load

I recently added some bootstrap templates to my resources/templates folder. However, when I start the server and navigate to the URL where I linked the index.html page, the bootstrap styling is not loading. Surprisingly, when I use the Chrome button in Int ...