Having trouble with menu alignment when implementing jQuery for extra features

I am in need of creating a mega menu similar to the one depicted in the image below.

So far, I have managed to make it work to some extent as shown in this jsFiddle link.

However, I am encountering some design and functionality issues with my current implementation.

For instance, when attempting to hide the default text for each dropdown menu using

//$(this).find(".nav-info").hide();
, Menu 4 & 5 do not appear correctly on the right side.

I aim to replicate a menu layout similar to the one featured on this website. However, I do not require the default text displayed for the parent menus.

Although I made modifications to the script for showing the first list item of the submenu, it works well for Parent menu ONE and TWO but causes alignment issues for MENU FOUR and FIVE.

I would greatly appreciate assistance in resolving this issue...

CODE

<div class="container_16">
    <div class="nav-main grid_16">
        <div class="wrap-nav-media">
            <ul id="nav-top-media">
                <!-- ONE -->
                <li class="nav-item-1"><a href="../company-overview">Parent Menu One</a>

                    ...

<p>UPDATE:</p>

<p>After attempting to hide the following HTML code block it breaks the alignment of MENU FOUR & FIVE when using <code>$(this).find(".nav-info").hide();

<div style="display: block;" class="menu-page nav-info">    <a class="thumb" rel="nofollow" title="" href="">
                                        <div style="height:80px width:80px; background-color:yellow;float:right;">IMAGE</div>
                                    </a>

    <div>
        <p>This is Default text when i try to hide this then this menu moves to left</p>
    </div>
</div>

Answer №1

After reviewing the website you mentioned as an example, I have come up with a solution using plain CSS and a few lines of jQuery to achieve the same functionality. Here is my approach:

HTML

<nav class="nav-wrapper">
    <ul class="nav">
        <li><a href="#">Menu One</a>
            <div  class="dropdown">
                <ul>
                    <li><a href="#">Sub menu one</a>
                        <div class="dd-panel">
                            <img class="media" src="http://lorempixel.com/200/160/animals/" alt="image" />
                            <p class="media-caption"><strong>Menu one - sub menu one</strong>Aliquam erat volutpat. Quisque porta varius tortor, ac luctus lorem condimentum vel. Mauris venenatis justo id fringilla tincidunt.</p>
                        </div>
                    </li>
                    ... (additional HTML code)
                </ul>
            </div>
        </li> 
        ... (more list items)
    </ul>
</nav>

CSS

body {
    font:normal 14px Arial, Sans-serif;
}

a {
    text-decoration:none;
}
ul {
    margin:0;
    padding:0;
}
... (CSS styling)

jQuery

$(document).ready(function(){
    $(".nav").on("mouseenter", " > li", function(){

        /*if dropdown is likely to go out of parent nav then right align it :) */
        if (($(this).offset().left) + 600 > $('.nav').width()) {
            $(this).find(".dropdown").addClass("dropdown-last");
        }
    });
    ... (jQuery functionality)
});

View the live example on JSFIDDLE: http://jsfiddle.net/shekhardesigner/wPWDm/

Answer №2

Hey @KnowledgeSeeker, your code looks great! Here's a suggestion to simplify it even further: instead of using show and hide, you can just have one global variable for each menu tab like this:

    var html_4=""; 
    and 
    var html_5=""; 

You can store the inner HTML code for each tab in these variables separately. Then, when switching tabs, you can use this code: (For tab four)

    $(this).find(".nav-info").html("");
    $(this).find(".nav-info").html(html_4);

Do the same for tab five by changing the variable name. Make sure to keep the class always because removing it may cause CSS issues to arise.

Answer №3

I recently implemented this using only CSS. It involves specific widths and heights, as well as absolute positioning for the details. This means that the popup boxes will not automatically adjust to fit their content.

Each detail needs a background because I always show the first item to simulate it being displayed already. When hovering over other items, they appear on top of the first one. Without a background, you would see the details of the first item behind them.

Here's the HTML code:

<ul class="dropdown-mega">
    <li class="dd-mega-item">
        <a href="#">Menu 1</a>
        <ul class="dd-mega-sub">
            <li class="dd-mega-sub-item">
                <a href="#">Submenu 1</a>
                <div class="dd-mega-sub-item-details">
                    <img src="http://placekitten.com/100/100">
                    <p>Details for Submenu 1</p>
                </div>
            </li>

            <!-- More li elements here -->

        </ul>
    </li>

    <!-- More li elements here -->

</ul>

And here's the corresponding CSS:

.dropdown-mega, .dropdown-mega ul {
    margin: 0;
    padding: 0;
}

/* More CSS styles for dropdown menu */

You can view a demo of this implementation on JSFiddle.

Answer №4

If you're looking for resources on creating mega menus, consider checking out Green Gecko Design's article at . Additionally, Nettus+ has a helpful guide on the topic:

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

Convert HTML to PDF using AJAX for seamless downloading

I am currently using html2pdf to generate PDFs from WordPress posts within a multi-site installation. The setup is working well and here is how it is structured: On my homepage, I have a link: <a target="_blank" id="downloadPDF" href="<?php echo ge ...

Exploring the Excitement of PHP Regular Expressions (preg_replace)

Looking for assistance with a regex preg_replace function to clean up HTML content being submitted to my app's controller/model. The goal is to strip away unwanted HTML and convert specific elements into proprietary tags for the app. $postText = $_PO ...

Add a fresh component when a key is pressed - reactjs

After successfully implementing the onKeyPress function, I am now trying to load a new component that I have created when the 'Enter' button is pressed. Here is the code snippet: class SearchBar extends Component { constructor(props ...

Sending non-textual data within a JQuery ajax POST request

I have encountered an issue related to sending data from a JavaScript application to a Node.js server for database query purposes. Despite trying to find a solution from various sources, I have been unable to resolve it. Here is the code snippet used to s ...

When the play button is clicked, the video slides over to the left

Whenever I attempt to click on the video link, it seems to shift over to the left side of the page. The link is positioned in the center and I would prefer for the video to open up at the same central location. However, it consistently opens up on the left ...

Exploring Raycasting with Three.js and WhitestormJS

Having trouble with Raycasting in Three.js and WhitestormJS. Perhaps I haven't grasped the concept properly. The goal is to align the raycaster's direction with the camera, so that when it intersects an element, that element is added to the inte ...

asynchronous ajax resolution

When I send multiple ajax requests simultaneously, the arrival time of the results is unpredictable and causing issues for me. Here's the problem I'm facing: Every time I click on a TAB button created with HTMLElement, it triggers an ajax call. ...

Unable to submit CSV file for Controller Action

I am encountering an issue with uploading a csv file to my backend action method. I have an action method called UploadPropertyCSV in Controller PropertyController that is supposed to process the file and add it to the database. However, when I click submi ...

Display an array comprising of other arrays

function PersonXYZ(fName, lName) { this.lastName = lName; this.firstName = fName; this.grades = []; this.grades.push([4, 67, 5]); this.grades.push([41, 63, 5]); this.grades.push([4, 67, 55]); } var person = new PersonXYZ('John', 'Doe&apos ...

Serializing data in JavaScript and inserting it into a database

After retrieving data from a form, var form_data = $('#form').serialize(); I receive a result such as name=bill&age=133&gender=male This data is then sent to a controller using ajax $.ajax({ type: "POST" ...

Converting rotation into a directional vector

I have a Three.js object and I am able to read its rotation. However, I am looking for a way to extract a vec3 that indicates the direction in which the object is currently rotated. Can someone provide guidance on how to achieve this? ...

Unfortunately, CSS3 PIE is not compatible with border-radius and box-shadow properties

I created a basic HTML/CSS code for testing purposes, but I'm having trouble getting the library to function properly. I've placed the .htc, .php and .js files in the same directory as index.html, but it just won't work. Check out the code ...

Tips for selecting a dynamic element with a variable in Javascript, jQuery, and DOM

I've been facing this issue for quite some time now. I have a function that takes an object as input, loops through it, and generates rows with display columns (using Bootstrap). To accomplish this, I'm utilizing jQuery to select an ID value from ...

Steps to transfer text from one input field to another by clicking a button using JavaScript

Hello, I am new to Javascript so please forgive me if my question seems silly. I have been tasked with creating a form containing two input fields and a button. When the button is clicked, the text entered in the first field should move to the second field ...

Utilizing Ajax to dynamically populate table columns

I have an HTML table with specific labels and styles. I am looking for a way to update the table by using the unique 'id' of each row/element and populate the data using Ajax. Can anyone provide a solution or suggestion? <table border="0" ...

What is the best way to access the data from a CSV file that has been uploaded using ngFile

Is there a way to use ngFileUpload to read the contents of a csv file before saving it to a database? $scope.checkAndUploadFile = function (file) { $scope.uploadFileProgress = true; Upload.upload({ method: 'POST', url: & ...

The unexpected behavior of string.matchAll(regex) in web browsers

Struggling to use a regular expression to match markdown in node.js, but facing issues when attempting to run it in a react.js app... const regex = /(\*\*\*|___|\*\*|\*|__|\[.*?\]\([^)]*\))(.*?)(\1|$ ...

The drop-down menu vanishes mysteriously before it can be selected

I'm having trouble making my pop-up menu touchable. It keeps disappearing. I've tried a few things but nothing seems to work: - Placing the display:none above the hover didn't make a difference. - Changing the position: absolute in #navbar ...

Redirecting to the personalized 404 error page

I searched high and low for a simple answer to my question, but came up empty. I currently have two additional websites, website 'A' and website 'B', within the structure of my main website (www.xyz.com). Each of these websites is locat ...

Creating a dynamic select functionality in Drupal forms

Being more focused on backend development, I am facing a challenge that may be simple for jQuery experts. In Drupal, I have two arrays - one containing names of views and the other having displays for each view. To populate these arrays, here is the code s ...