Error in jQuery submenu positioning issue

I am attempting to design a menu that opens when the parent element is clicked and closes when the mouse leaves the previously opened one. It should operate from left to right.

Here is an example:

    <ul class="menuFirst">
        <li><img src="img/ico1.png"></li>
            <ul class="">
                <li><img src="img/ico2.png" alt="submodule1"></li>
                <li><img src="img/ico2.png" alt="submodule2"></li>
                <li><img src="img/ico2.png" alt="submodule3"></li>
            </ul>
        <li><img src="img/ico1.png"></li>
            <ul class="menuSecond">
                <li><img src="img/ico2.png" alt="submodule1"></li>
                <li><img src="img/ico2.png" alt="submodule2"></li>
                <li><img src="img/ico2.png" alt="submodule3"></li>
            </ul>
        <li><img src="img/ico1.png"></li>
            <ul class="menuSecond">
                <li><img src="img/ico2.png" alt="submodule1"></li>
                <li><img src="img/ico2.png" alt="submodule2"></li>
                <li><img src="img/ico2.png" alt="submodule3"></li>
            </ul>
    </ul>

Here is the corresponding CSS:

ul.menuFirst{
    list-style-type: none;
border-top: #AAA solid 3px;
border-right: #AAA solid 3px;
border-bottom: #AAA solid 2px;
position: absolute;
left: 0px;
top: 70px;}
    ul.menuFirst li{
    background: #DDD;
    border-bottom: #AAA solid 1px;
}
ul.menuFirst li img{
margin:5px;
}
ul.menuFirst ul{
display: none;
}
    ul.menuFirst ul.menuSecond {
list-style: none;
display: table;
position: absolute;
left: 30px;
    }
    ul.menuFirst ul.menuSecond li{
display: table-cell;
    }

And here is the jQuery implementation:

<script>
$(document).ready(function(){
    $("ul.menuFirst li").click(function(){
        $(this).next().toggle();            
        var ypos= $(this).next().position();
        alert(ypos.top);
        $(this).next().css('top','-=38');

        $('ul.menuFirst ul.menuSecond').one('mouseleave',function(){
        $(this).css('top', '+=38').toggle();
    });
    });
});

It currently works for the first element but it has some bugs. When trying to add .animate(), it does not function as intended. I have decided to debug this issue before proceeding with the animation, but I am unsure how to do so. The final result should look like this:

Answer №1

To fix your HTML code, please use the following:

<ul class="menuFirst">
    <li><img src="img/ico1.png">
        <ul class="">
            <li><img src="img/ico2.png" alt="submodul1"></li>
            <li><img src="img/ico2.png" alt="submodul2"></li>
            <li><img src="img/ico2.png" alt="submodul3"></li>
        </ul>
    </li>
    <li><img src="img/ico1.png">
        <ul class="menuSecond">
            <li><img src="img/ico2.png" alt="submodul1"></li>
            <li><img src="img/ico2.png" alt="submodul2"></li>
            <li><img src="img/ico2.png" alt="submodul3"></li>
    </ul>
        </li>
    <li><img src="img/ico1.png">
        <ul class="menuSecond">
            <li><img src="img/ico2.png" alt="submodul1"></li>
            <li><img src="img/ico2.png" alt="submodul2"></li>
            <li><img src="img/ico2.png" alt="submodul3"></li>
        </ul>
    </li>
</ul>

Answer №2

Alright, I have resolved the issue...

    $('ul.menuFirst ul').each(function(){
        $(this).css('top', '-=38').hide();
    });

This code runs when the DOM is ready so that the position does not need to be changed each time it is opened or closed.

        $("ul.menuFirst li").click(function(){
        $(this).next().toggle("slide", 
                 {direction: "left"})
        });

        $('ul.menuFirst ul.menuSecond').mouseleave(function(){
        $(this).hide("slide", 
                 {direction: "left"});

Subsequently, I am using this method to animate it.

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

Using curly brackets as function parameters

Can someone help me understand how to pass an emailID as a second parameter in curly braces as a function parameter and then access it in AccountMenuSidebar? I apologize for asking such a basic question, I am new to JavaScript and React. class Invoices ex ...

Update the input value to replace duplicate values in the Chrome Android 6x version

Recently, I encountered an issue with my Samsung Galaxy Note 4. I have been working with jQuery and attempting to write code that will automatically format input text into a specific pattern when a person fills in a value (using keyup event). For examp ...

Guide on inserting tooltip to designated header column in primeNG data table

Html <p-table #dt1 [columns]="cols" [value]="cars1"> <ng-template pTemplate="header" let-columns> <tr> <th *ngFor="let col of columns"> {{col.header}} </th> ...

NuxtJS Static generated HTML page fails to load JavaScript while accessing /index.html

I'm currently working on a project using Nuxt.js to generate static content, which involves utilizing JavaScript for tasks such as navigation and form functionality. Everything works smoothly when running the page with npm run dev. However, after ex ...

Looking to deactivate a particular checkbox in a chosen mode while expanding the tree branches

I encountered an issue with a checkbox tree view where I needed to disable the first two checkboxes in selected mode. While I was able to achieve this using the checked and readonly properties, I found that I could still uncheck the checkboxes, which is no ...

Code not functioning properly in Internet Explorer

In one of my JavaScript functions, I have the following CSS line which works well in all browsers except for IE (Internet Explorer). When the page loads, the height of the element is only about 4px. element.setAttribute('style', "height: 15px;") ...

What could be causing my webpage content to be partially hidden by my CSS menu?

I am currently working on my website which features a CSS menu with my website logo, a text box, and a dropdown. Additionally, I have created a login form using a table like the one shown below. <p>&nbsp;</p><table width="40%" border="0 ...

Issue with Ng-Messages not functioning properly due to a custom ng-Include directive lacking a child scope

I have created a custom directive called lobInclude, which is similar to ngInclude but with no isolated scope: .directive("lobInclude", ["$templateRequest", "$compile", function($templateRequest, $compile) { return { restrict: "A", ...

Eliminate items from one array when there is a corresponding match in a separate array using JavaScript

I am working with 2 JavaScript arrays First Array var columns=[{name: 'id', default: true}, {name: 'type', default: true},{name: 'typeName', default: true}, {name: 'client', default: false}]; Second Array var unSel ...

Submitting a form using an anchor tag in Angular 8: A step-by-step guide

I have a question about how to submit form data using hidden input fields when a user clicks on an <a> tag. <form action="/submit/form/link"> <input type="hidden" [attr.value]="orderNumber.id" /> <input type="hidden" [attr.value]= ...

What is the best way to send a JavaScript array to a Perl script using AJAX?

Is it possible to convert a JavaScript array passed via AJAX into a Perl array? Accessing in Perl: @searchType = $cgi->param('searchType'); print @searchType[0]; Result: employee,admin,users,accounts It appears that the first value in the ...

Using JavaScript to calculate dimensions based on the viewport's width and height

I have been trying to establish a responsive point in my mobile Webview by implementing the following JavaScript code: var w = window.innerWidth-40; var h = window.innerHeight-100; So far, this solution has been working effectively. However, I noticed th ...

Arranging divs parallelly while incorporating components within

Striving to create a layout with 3 divs side by side in an HTML document, my initial attempt resulted in this: https://i.stack.imgur.com/CpdLX.png However, I encountered an issue where the div would shift down whenever text or other objects were added: ...

Guide to setting up a trigger/alert to activate every 5 minutes using Angular

limitExceed(params: any) { params.forEach((data: any) => { if (data.humidity === 100) { this.createNotification('warning', data.sensor, false); } else if (data.humidity >= 67 && data.humidity <= 99.99) { ...

TypeScript encountered an unexpected { token, causing a SyntaxError

Despite multiple attempts, I can't seem to successfully run my program using the command "node main.js" as it keeps showing the error message "SyntaxError: Unexpected token {" D:\Visual Studio Code Projects\ts-hello>node main.js D:&bsol ...

Vue.js: Issue with updating list in parent component when using child component

I'm encountering an issue when utilizing a child component, the list fails to update based on a prop that is passed into it. When there are changes in the comments array data, the list does not reflect those updates if it uses the child component < ...

Ways to apply CSS to a changing div ID

I am looking to apply CSS to a dynamically generated div ID. var status = var status = item.down().next().innerHtml(); if(status == "test") { var c = 'item_'+i ; c.style.backgroundColor = 'rgb(255, 125, 115)'; //'ite ...

Experiencing trouble with setting values in JavaScript?

I have my code set up to display numbers 170 and 122 using Javascript, but I'm not seeing the expected output. <!DOCTYPE html> <html> <body> <button onclick="UidToPost()">Get It</button> <script> ...

Incorporate a customizable month option within a flexible calendar design

I'm working on creating a calendar that adjusts to different screen sizes for my website. The script I've implemented is as follows: <script type="text/javascript"> $(document).ready(function () { $(".responsive-calendar").responsiv ...

Saving and accessing AJAX XML responses in sessionStorage

I have encountered an issue with storing XML response from ajax into sessionStorage. Despite successfully setting the data, I am unable to retrieve it. $.ajax({ url: '/webservice.svc/getProfile', data: { "memberId": $(authenticat ...