If the list item is the first or second child, align the menu to the right

I've been facing some challenges with my mega menu implementation. The dropdown appears either on the left or right side based on calculations of offset.left, but it seems to behave differently across various screen resolutions.

As a solution, I'm considering displaying the First and Second dropdown menus on the right side and the rest on the left. However, I'm unsure how to incorporate the nth-child element logic into an if statement.

Here's a simple example in this Fiddle: http://jsfiddle.net/42MfQ/1/

You can view my current mega menu sample that has issues like this one here: http://jsfiddle.net/5eecT/13/

Your assistance in getting the script to display the menus correctly would be greatly appreciated.

At present, the code is not functioning as intended. Here's a snippet illustrating part of the problematic code:

if (($(this).offset().left) + 200 > $('.menu-wrapper').width()) {
    $(this).find(".dropdown").addClass("dropdown-last");
}

Considering this issue, I propose displaying the first two dropdown menus on the right side and the rest on the left to avoid incorrect auto-detection.

I am seeking guidance on how to write this particular code block:

   if($( "li:nth-child(1)" ) == true || "li:nth-child(2)" == true ))
   {
     alert('show dd menu on right side of parent menu');  
   }

Otherwise, { alert('show dd menu on left side of parent menu');
}

Answer №1

After encountering unexpected results, I decided to implement the following jQuery code to ensure it functions correctly regardless of the outcome.

This entails adding a dropdown-last class to align the submenu on the left side of the text. Below is an illustration along with instructions for customization:

$("#menu").on("mouseenter", ":nth-child(4)", function () { $(this).find(".dropdown").addClass("dropdown-last"); });
$("#menu").on("mouseenter", ":nth-child(5)", function () { $(this).find(".dropdown").addClass("dropdown-last"); });
$("#menu").on("mouseenter", ":nth-child(6)", function () { $(this).find(".dropdown").addClass("dropdown-last"); });

I encountered discrepancies in display across different screen resolutions due to the initial jQuery implementation.

if (($(this).offset().left) + 200 > $('.menu-wrapper').width()) {
    $(this).find(".dropdown").addClass("dropdown-last");
}

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

Submitting and processing checkboxes in HTML using PHP

I've been struggling to figure out how to insert checked checkboxes or unchecked ones into the database using PHP. I've tried many different methods, but none seem to be working. I feel like I'm very close to solving it but can't pinpo ...

Retrieve a subset of fields from a collection of nested documents

My document looks like this: { "_id" : "2", "account" : "1234", "positions" : { "APPL" : { "quantity" : "13", "direction" : "long" }, "GOOG" : { "quantity" : "24", "direction" : "long" } } } I want to retrieve the entire positions object, but only ...

Is there a way to create triangles on all four corners of an image using canvas in JavaScript?

In my code, I am currently working on drawing rectangles on the corners of an image using JavaScript. To achieve this, I first extract the image data from a canvas element. // Get image data let imgData = ctx.getImageData(0, 0, canvas.width, canvas.height) ...

What steps can I take to get rid of the 'Optimize CSS Delivery' notification on my website?

Utilizing Google's PageSpeed Insights to analyze my website's speed, I have identified an issue: Your page currently has 11 blocking CSS resources, resulting in slower rendering time. About 5% of the content above-the-fold could be displayed with ...

Is it possible to show one element while hiding others upon clicking using JavaScript?

Concept My idea is to create a website with a navigation menu where only one section is visible at a time. Each section would become visible upon clicking a specific button in the navigation bar. Challenge I attempted to achieve this using the following ...

Why are link elements that generate hyperlinks important?

I am confused about the purpose of using link tags to create hyperlinks. Is there a reason to include code like <link rel="author" href="about.html">, <link rel="next" href="next.html">, or <link rel="help" href="help.html">? Since this ...

How can I verify if an input field matches a CNIC Number using PHP?

I need help implementing a regular expression to validate CNIC numbers in an input field. Here is the pattern I want to match: 12345-1234567-1 If the user enters something that matches this pattern, it should display "valid" in a specific span element. If ...

Div's external dimension not being computed

I am attempting to calculate the overall height of the div content in order to expand the sticky-container and create the appearance that the image is tracking the user. Unfortunately, using outerHeight does not seem to be effective for some reason. While ...

Starting the node server using the port value specified in the .env file results in an error when attempting to restart the server

I have built a simple node application and specified the port number in the .env file. However, I am encountering an issue where when using app.listen(process.env.PORT,()=>{}), the server runs successfully the first time but when attempting to run it ag ...

Issues of unfulfilled requirements in the recent npm (and node) updates

After updating npm and node to the latest versions, I am encountering the following errors and warnings: $ npm --version 2.4.1 $ node --version v0.10.36 $ npm install > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f895 ...

trouble with fetching data

Within my Backbone view, I have the following code snippet: var BookView = Backbone.View.extend({ initialize: function() { this.render(); }, render: function() { this.model.fetch({ success : function(model, resp, opt) { alert(this.$el. ...

What is the best way to generate an extensive table with HTML and AngularJS?

I am looking to implement an expandable table feature where a single header with a plus icon can be clicked to reveal the child rows beneath it. Currently, I have been able to achieve this functionality. However, I am facing an issue where the child rows ...

Styling web pages with HTML and CSS directly from a MySQL

I'm facing a challenge in crafting a solution for a intricate issue. My project involves building a website that generates HTML templates containing both CSS and HTML files. Users will have the ability to create multiple templates/sites. In an effort ...

Tips for creating horizontal scrolling in the div element

I'm working with a div element that looks like this: <div id="tl" style="float:right;width: 400px; height:100px; background-color:Green; overflow-x: scroll;overflow-y: hidden;"> <div id='demo' style="float:left;height ...

Having trouble customizing the appearance of the active state for the close button in jQueryUI's Dialog

I am trying to customize the active state of the close button in the title bar of a jqueryUI dialog. While I have successfully styled its normal and hover states, the active state doesn't seem to respond as expected. Could there be a feature in the p ...

Identify the UNFINISHED section of a Bootstrap Progress Bar

Is there a way to customize the color and add labels to both the complete and incomplete sections of a progress bar in Bootstrap 3? Here is an example: https://i.sstatic.net/F8iEq.png I understand that I can set a CSS background for the entire .progress ...

Ensuring a fixed table with vertical scrolling only, no horizontal scrolling, while scrolling in either direction

I'm working with an HTML fixed design that contains a center-aligned table with extensive data scrollable both vertically and horizontally. To address the issue of keeping column headers visible when scrolling vertically, I used jQuery's clone() ...

Click functionality in jQuery triggers multiple times within a click event

I inserted code samples into this link In the result section of jsfiddle, I created three accordion sliders with the names: Home, About, and Contact On clicking the Delete It button in the Home slider, it logs to console.log 3 times. When clicked on the ...

unique navbar color transition effect specifically for the homepage

I have successfully created a function called changeColour in my code that changes the color of the navbar when scrolling past a certain point. However, I am encountering an issue where I only want this effect to be applied on the Home page. I would like a ...

What is the best way to programmatically change the event tied to an element's click

<div id="filters"></div> <div id="available"> <span class="badge badge-pill" onclick="addFilter()">Label</span> </div> function addFilter(event) { document.getElementById("filters").appendChild(eve ...