The dropdown menu is set to dynamically change its opening direction from right to left based on the width

I am in the process of creating a recursive menu bar that contains multiple layers of ul li elements. The default behavior is for the menu to expand from left to right.

However, if the browser width is too small to accommodate the open menu items, they extend off the right side of the screen.

To address this issue, I want the direction of menu item expansion to switch from right to left when the browser width is insufficient.

Although I have attempted to implement this functionality using jQuery, the code is not functioning as intended.

The current issues are:

  • If the browser width is smaller than the menu, the menu directions change but there are some bugs,
  • If the browser size is increased after shrinking, the menu width does not adjust back to 100% without refreshing the browser!

My JavaScript code snippet:

$(document).ready(function() {
    $(".anamenu li").on('mouseenter mouseleave', function(e) {
        e.preventDefault;
        if ($('ul', this).length) {
            var elm = $('ul:last', this);
            var off = elm.offset();
            var l = off.left;
            var w = elm.width();
            var docH = $(".disgovde").height();
            var docW = $(".disgovde").width();

            var isEntirelyVisible = (l + w <= docW);

            if (!isEntirelyVisible) {

                $("ul.anamenu li ul ul > li ul").css({
                    "left": "auto",
                    "right": "100%",
                });

                $("ul.anamenu li ul").css({
                    "left": "auto",
                    "right": "0",
                });
                $("ul.anamenu li > ul ul").css({
                    "left": "200px",
                    "right": "0",
                });
                $("ul.anamenu > li ul ul").css({
                    "left": "auto",
                    "right": "100%",
                });

            } else {
                $("ul.anamenu  li ul ul > li ul").css({
                    "left": "200px",
                    "right": "0",
                });


            }
        }
    });
});

CSS styling snippet:

*,
*::before,
*::after {
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
}


a:hover {
    text-decoration: none;
}
body {
    height: 100vh;
    /*
    display: flex;

    flex-direction: column;
    align-items: center;
    align-content: center;
    justify-content: flex-start;
    */
}

[.....]
[... Other HTML and CSS code snippets ...]

Answer №1

To create a responsive design, it is recommended to utilize a media query to adjust styles based on screen width.

Adhering to the mobile-first approach, customize the dropdown menu to open vertically by default and then implement a media query in your CSS code like below:

@media screen and (min-width: 768px) {
  .anamenu ul {
    // Update dropdown styling to open horizontally.
  }
}

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

Hide the li tag by using a class to make it disappear

I'm looking to hide the dropdown class, which will only be used for specific li tags. I attempted to do this with the following CSS, but it didn't work: .dropdown{ display:none; } Is there a correct way to achieve this? Am I missing somethi ...

chaotic telerik editor arrangement

I have incorporated the Rad Editor into my ASP.NET page. <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <telerik:RadEditor ID="reSummery" runat="server" > </telerik:RadEditor> Despite not referencing ...

What is the process to discover, upload, and implement a custom font?

In my quest to locate and incorporate a font named neontubes, or if not available, a font called freestyle script, I have learned that I must obtain an src which is then included in my style.css file and subsequently designated in the styling rule like s ...

Tips on Including a Custom Header in an Ajax Request for a Cross-Domain JsonP Call

Is there a way to add a custom header using an ajax call in jQuery for a cross-domain JSONP call? I am making a web service call in an HTML page using Ajax cross-domain call. I am currently using JSONP and now need to send some parameters in the header. ...

Issue with menu display on Internet Explorer

Having some trouble with IE. My menu displays fine in Firefox and Chrome, but IE is causing issues... It seems that IE isn't rendering the <li> tag properly, so my dropdown menu isn't functioning in IE. Can anyone assist me? Here's t ...

Prevent the previous function from executing using Jquery

$(document).on('touchstart click', function (){ $('#box').hide(); }); if(isset($_GET['s'])){include(//another page);} By including another page, the document click functionality $('#box').hide(); will be di ...

Using jQuery to assign each name the value of the group of radio buttons

For instance: In the database, there are currently five names available (possibly more): ANGGIE, BOB, CHUCK, DEAN, EAST. Here is the HTML code snippet: <form name = "test"> <?php $num++; ?> <td> [each name in the database] </td> ...

Execute script when on a specific webpage AND navigating away from another specific webpage

I created a CSS code that adds a fade-in effect to the title of my website, and it works perfectly. However, I now want to customize the fade-in and fade-out effect based on the page I am transitioning from. My desired outcome: If I am leaving the "Biolo ...

Populating Hidden Form Fields with Dynamic Identifiers

I'm facing an issue with a form where I need to input a value into a hidden field upon clicking a button. Despite using unique IDs for all elements, the data is not being submitted in the POST request. The IDs are unique because there are multiple f ...

Tips for effectively extracting information from a website using R

Looking for advice on scraping this web page with ASP and passing parameters? Take a look at the URL: I've made an attempt using the code below, but it's not as efficient as I'd like it to be (the results are not accurate, especially with m ...

Showing hyperlinks in a PHP SQL Management system website depending on specific criteria

After developing a database and importing it to PhpMyAdmin, I have also set up an SQL Database Management Website and loaded it into Wampserver using old university resources. The purpose of my database is to showcase artists that I admire and their music ...

Implementing a JQuery click method within a class structure

I'm having trouble getting the functionality of the .click function to work on my page unless I paste it into the browser console. In my class, this is what I have: var myClass = function(){ var toggleChecked = function(){ $('#myCheck ...

Decompressing CSS in PhpStorm

I'm currently using PhpStorm version 2016.3.2. Occasionally, I come across <style> tags in my HTML files containing minified CSS code. Is there a way to create a shortcut for unminifying the code within these tags? For instance, the following ...

Guide to emphasizing a specific term within a string by utilizing coordinates in javascript

I am facing a challenge when trying to highlight multiple words within a sentence. Specifically, I have text data that looks like this: "The furnishing of Ci Suo 's home astonished the visitors: a home of 5 earthen and wooden structures, it has a sit ...

Issue with AngularJS ng-if causing hidden elements to not respond to ng-select event

In my code, there is a hidden select input that is initially hidden using ng-if. Inside this hidden select, I have included a ng-change tag, which triggers a function to display an img element that is also hidden by ng-if. The element hiddenNinja is hidd ...

What is the best way to efficiently set up a scrolling text ticker for repeated use?

I am currently utilizing GreenSock/TweenMax for the creation of scrolling text, inspired by the design seen on this webpage: If you're interested in learning more about Greensock and its capabilities, take a look at their documentation here: While I ...

Tips for aligning a pseudoElement in the center below a bootstrap navigation link

Hello, I'm trying to align the line under my menu that was created using a pseudo element. Here is the HTML code: .nav-link.active { color: #482683; font-size: 14px; } .nav-link.active::after { content: ''; display: b ...

"The Font Awesome icons seem to be missing from the display, and I'm unsure of

<html lang="en"> <head> <link rel="stylesheet" href="style.css"> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script ...

The two divs are positioned on top of one another, with the link in the bottom div being unclickable

I am trying to create a unique effect where a tile divides into two on hover, with each tile acting as an individual link. Additionally, I want the background color of the tiles to change on hover. To achieve this, I have stacked two divs (toptile and bot ...

What is the best way to ensure the current tab remains unchanged when updating a page?

I have a unique horizontal tab system displayed as follows: <a id="default" class="link" href="#">Tab 1</a> <a class="link" href="#">Tab 2</a> <a class="link" href="#">Tab 3</a> This tab system is created using basic H ...