Conflicting jQuery slide effects and jQuery UI positioning causing issues

As you hover over an element, the position function adjusts its left attribute to the correct value. However, when you move the mouse away, it resets back to 0, causing the element to appear below the first one in the list.

This issue is noticeable when you hover over and leave any list element in the navigation bar, except for the first one.

Check out the code snippet below:

$(document).ready(function() {
$('.nav_item').mouseenter(function() {
var listNode = $(this);

// must be called first before the position call (no clue why)
$('#nav_sub_list').stop().slideDown('normal');

$('#nav_sub_list').position({
of: listNode,
my: "left top",
at: "left bottom",
collision: "fit none"
});

// left will not be 0 for all <li> elements except the first
console.log($('#nav_sub_list').position());
console.log("mouse in");
});

$('#nav_sub_list').mouseout(function() {
// will reset left back to 0
console.log($('#nav_sub_list').position());

$('#nav_sub_list').stop().slideUp('normal');

// left will be 0
console.log($('#nav_sub_list').position());
console.log("mouse out");
});
});
#nav_sub_list {
position: absolute;
top: 40px;
width: 200px;
height: 200px;
background-color: pink;
display: none;
}

ul.h_btn_list li { 
display: inline;
padding: 10px;
color: white;
width: 100%;
font-weight: bold;
}

li.nav_item {
position: relative;
}

#nav_bar {
position: relative;
z-index: 2;
}

#nav_bar ul {
margin: 0px 5%;
background-color: #005eff;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
border: 3px transparent;
padding: 10px;
float: right;
-webkit-box-shadow: 0px 0px 4px 2px rgba(0,0,0,0.57);
-moz-box-shadow: 0px 0px 4px 2px rgba(0,0,0,0.57);
box-shadow: 0px 0px 4px 2px rgba(0,0,0,0.57);
}
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<div id="nav_bar">
<ul class="h_btn_list">
<li class="nav_item">Home
<div id="nav_sub_list"></div>
</li>
<li class="nav_item">Community</li>
<li class="nav_item">Downloads</li>
<li class="nav_item">Contact Us</li>
</ul>
</div>

Thank you for reviewing my code and providing assistance!

Answer №1

The issue with the code not functioning correctly is due to the fact that the "#nav_sub_list" div is nested inside the first li tag in the HTML. This causes the

$('.nav_item').mouseenter(function() {
code to execute when hovering over the "#nav_sub_list".

To resolve this issue, moving the

<div id="nav_sub_list"></div>
outside of the li tag will fix the problem.

$(document).ready(function() {
$('.nav_item').mouseenter(function() {
var listNode = $(this);

// must be called first before the position call (no clue why)
$('#nav_sub_list').stop().slideDown('normal');

$('#nav_sub_list').position({
of: listNode,
my: "left top",
at: "left bottom",
collision: "fit none"
});

// left will not be 0 for all <li> elements except the first
console.log($('#nav_sub_list').position());
console.log("mouse in");
});

$('#nav_sub_list').mouseout(function() {
// will reset left back to 0
console.log($('#nav_sub_list').position());

$('#nav_sub_list').stop().slideUp('normal');

// left will be 0
console.log($('#nav_sub_list').position());
console.log("mouse out");
});
});
#nav_sub_list {
position: absolute;
top: 40px;
width: 200px;
height: 200px;
background-color: pink;
display: none;
}

ul.h_btn_list li { 
display: inline;
padding: 10px;
color: white;
width: 100%;
font-weight: bold;
}

li.nav_item {
position: relative;
}

#nav_bar {
position: relative;
z-index: 2;
}

#nav_bar ul {
margin: 0px 5%;
background-color: #005eff;
border-bottom-left-radius: 10px;
border-bottom-right-radius: 10px;
border: 3px transparent;
padding: 10px;
float: right;
-webkit-box-shadow: 0px 0px 4px 2px rgba(0,0,0,0.57);
-moz-box-shadow: 0px 0px 4px 2px rgba(0,0,0,0.57);
box-shadow: 0px 0px 4px 2px rgba(0,0,0,0.57);
}
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<div id="nav_bar">
<ul class="h_btn_list">
<li class="nav_item">Home

</li>
<li class="nav_item">Community</li>
<li class="nav_item">Downloads</li>
<li class="nav_item">Contact Us</li>
</ul>
</div>
<div id="nav_sub_list"></div>

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

How deep can nesting go within a CSS rule?

When working with stylesheets, the majority of CSS rules follow a structured format: selector { property_1 : value_1; . . . property_n : value_n; } Each selector typically has only one {} block attached to it (without any sub {} blocks ...

I am experiencing issues with the customsort function when trying to sort a column of

Seeking assistance with customizing the sorting function for a Date column in a primeng table. Currently, the column is displaying data formatted as 'hh:mm a' and not sorting correctly (e.g. sorting as 1am, 1pm, 10am, 10pm instead of in chronolog ...

Tips for troubleshooting an Express application launched by nodemon through a Gulpfile in WebStorm 10

I have a unique Express application that is powered by a Gulpfile configuration. gulpfile.js 'use strict'; var gulp = require('gulp'); var sass = require('gulp-sass'); var prefix = require('gulp-autoprefixer'); va ...

What can be done to improve the elegance of this jQuery function?

I am facing an issue on my webpage where I have a drop-down select box with a default value of (empty). The problem arises when the user selects an entry, and the appropriate form for that entry type should be displayed. However, there are a couple of iss ...

Using Javascript Regular Expressions to validate that the first number in an amount is non-zero

Need a solution to only accept numbers that do not start with zero, but can contain zeros after the first digit. Currently using var.replace(/[^1-9]/g, ''); which prevents input of 0 altogether. Examples of valid inputs: 10 9990 Examples of in ...

Building an HTML form to generate an array of objects by collecting form data

Hey there, I'm working on an HTML form in React that utilizes form action and FormData. However, when I extract the formData using my method, it shows a structure like this: https://i.stack.imgur.com/nzgLX.png My goal is to have an array of objects ...

ReactJS: Dealing with issues in setting a dynamic index for the setState key

I have been attempting to utilize setState within a for loop using index in the following manner: for (var i = 0; i <= 9; i++) { this.setState({ location_option[i]: resourceData.location_option+i, location_option[i]_type: resourceData.locatio ...

Adjust the dimensions of the text area to modify its height and width

Can someone help me adjust the width and height of the <textarea> tag in HTML so that it only has two lines (rows)? Right now, it's only displaying one line. Any suggestions on how to solve this issue? Even when I try changing the width to 1000 ...

What could possibly be causing my code to execute multiple times?

Within my code, I have the following function that is triggered when the document is ready: $('#file_upload_form').submit(function(){ // show loader [optional line] //if(document.getElementById('upload_frame') == ...

Executing PHP function with JQuery's .ajax() function

My goal is to utilize a PHP function to scan a server directory for images. Once the images are found, I want to use JQuery's .ajax() to pass them back for a slideshow. The reason behind using the PHP function is to make it easier for users to add or ...

Utilize the dynamic backlash/Werkzeug debugger during an unsuccessful AJAX request on a TurboGears server

TurboGears offers the unique feature of "backlash," an interactive debugger within the browser that is built on the Werkzeug Debugger. When server-side debugging is enabled, in case of a request failure, an interactive web page is displayed where users can ...

The CSS border-radius feature shapes the corners without affecting the background

When I apply a 15px rounded border to a div, the border appears rounded on the outside but the corners where the border meets the background remain square. Here is an example: http://jsfiddle.net/BQT5F/1/ I tried using the background-clip property to add ...

Submitting information to a server

I have a simple Angular 6 app with follow and unfollow buttons. When you click follow, the number increases. I want to save these follower numbers to a JSON server. Here is the link to the JSON server documentation: JSON Server Documentation To see a dem ...

Error: The system reference 'Sys' is not defined in ASP.NET

I am trying to implement ajax functionality on the ASP.NET platform. To achieve this, I am using ScriptManager in conjunction with jQuery, and adding the script after the "document is ready" event. $(document).ready(function () { // sync {{scopeN ...

Utilize Mapbox-GL.JS to animate several points along designated routes

I'm encountering issues with the following example: Animate a point along a route My goal is to add another point and routes in the same map container. Here's what I've tried so far: mapboxgl.accessToken = 'pk.eyJ1IjoicGFwYWJ1Y2t ...

Tips for adjusting the button color in Material UI by utilizing the ":active" pseudo-class

In the project I am currently working on, I am incorporating Material UI. One of the tasks I need to complete is changing the active state of a button. I have implemented both hover and active states from Material UI in my code: const useStyles = makeStyle ...

The outputstring encountered an issue with the HTML class

Struggling a bit with php, I'm trying to incorporate a basic comment system that accesses and writes to a comments.php file. Everything is functioning well until I attempt to style the $outputstring. Here's the code in question: $outputstring ...

Generating a vertical bar adjacent to a bullet point in a list

Is there a way to add a vertical line next to each list item (except the last) without adding a new div? Adding a border line added an extra one that I didn't want. Sample HTML: <ul class="list"> <li><span id = "home">H ...

`Adjusting function calls based on the specific situation`

On my webpage, I have implemented a tab system where clicking on a tab displays the corresponding content below. This functionality is controlled by a JavaScript/jQuery function called 'changeTab'. Now, I want to set up individual JavaScript fun ...

How to use AngularJS directives to replace text with stars (*) in HTML

I am in need of a textarea control that has the ability to be masked, meaning that the text displayed should appear as stars instead of the actual characters. Since I might have multiple textareas in my form, saving the actual text in one variable and usi ...