Expanding the width of the hovered item to accommodate all its content

I'm currently working on a drop-down navigation bar, and I've encountered an issue. Check out the code here

My goal is to have the navigation container's height adjust dynamically when a user hovers over a menu item with children items. While I have achieved this functionality, the problem lies in the hovered item expanding in width to accommodate all its content. How can I prevent this from happening?

This is the HTML code snippet I am using:

<div class="container" id="menu-container">
<ul class="main-menu clearfix">
    <li class="dropdown"><a href="">Shop</a>
        <ul class="menu">
            <li class="dropdown float-child-to-right"><a href="">Filter by</a>
                <ul class="menu float-to-right">
                    <li class="dropdown"><a href="">Products</a>
                        <ul class="menu block-element">
                            <li><a href="">Guestbooks</a></li>
                            <li><a href="">Notebooks</a></li>
                        </ul>
                    </li>
                    <li class="dropdown block-element"><a href="">Collections</a>
                        <ul class="menu">
                            <li><a href="">Handle it</a></li>
                            <li><a href="">Tuff Love</a></li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li><a href="#">Contacts</a></li>
</ul>
<div class="clear"></div>

CSS styling applied:

 * {
    margin: 0px;
    padding: 0px;
 }

 ul {
    list-style: none;
 }
.container {
    border-bottom: #dedede 1px solid;
}
.clearfix:after { 
   content: " ";
   display: block; 
   height: 0; 
  clear: both;
}

 .main-menu li {

     float: left;
     min-width: 100px;

 }

.block-element li {
     float: none;
     display: block;
}

.float-to-right {
    float: right;
}

.main-menu li ul {
     /*display: none;*/
}

 .main-menu > ul {
     position: relative;
 }

Here is the JavaScript used:

(function($){ 

        $.fn.recurse = function (_this,  _parent_dropdowns ) {


            _parent_dropdowns.each(function () {

                $(this).mouseover(function ( event ) {                  

                    var _uls = $(this).children('ul.menu');

                    _uls.each( function () {

                        //has float-to-right class
                        if ($(this).hasClass('float-to-right')) {

                        }

                    });

                    $(this).children('ul.menu').css({ display: 'block' });

                });

                $(this).mouseleave( function ( event ) {

                    event.preventDefault();
                    event.stopPropagation();

                    _this.children('li').find('ul.menu').each( function () {
                        $(this).css({ display: 'none' });
                    });


                });



                _this.recurse(_this, _parent_dropdowns.find('li.dropdowns'))
            });

        }

        $.fn.menufy = function () {

            return this.each(function () {

                var _parent_menu = $(this);

                var _parent_dropdowns = _parent_menu.find('li.dropdown');

                _parent_menu.recurse(_parent_menu, _parent_dropdowns);


            });

        }

    })(jQuery);

    jQuery(document).ready(function ($) {
        $('.main-menu').menufy();
    });

Your assistance on this matter would be greatly appreciated!

Answer №1

Similar to that

(function($){ 

$.fn.recurse = function (_this,  _parent_dropdowns ) {


_parent_dropdowns.each(function () {

$(this).mouseover(function ( event ) {

var _uls = $(this).children('ul.menu');

_uls.each( function () {

//has float-to-right class
if ($(this).hasClass('float-to-right')) {

}

});

$(this).children('ul.menu').css({ display: 'block' });

});

$(this).mouseleave( function ( event ) {

event.preventDefault();
event.stopPropagation();

_this.children('li').find('ul.menu').each( function () {
$(this).css({ display: 'none' });
});


});
_this.recurse(_this, _parent_dropdowns.find('li.dropdowns'));
          
});

}

$.fn.menufy = function () {

return this.each(function () {

var _parent_menu = $(this);

var _parent_dropdowns = _parent_menu.find('li.dropdown');

_parent_menu.recurse(_parent_menu, _parent_dropdowns);


});

}

})(jQuery);

jQuery(document).ready(function ($) {
$('.main-menu').menufy();
});
 * {
 margin: 0px;
 padding: 0px;
 }

 ul {
 list-style: none;
 }
.container {
border-bottom: #dedede 1px solid;
}
.clearfix:after { 
   content: " ";
   display: block; 
   height: 0; 
   clear: both;
}

.main-menu li {

float: left;
min-width: 100px;

}

.block-element li {
float: none;
display: block;
}

.float-to-right {
float: right;
}

.main-menu li ul {
display: none;
}

.main-menu > ul {
position: relative;
}
.shop{width:50%;float:left;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container" id="menu-container">
<ul class="main-menu clearfix">
<li class="dropdown shop"><a href="">Shop</a>
<ul class="menu">
<li class="dropdown shop"><a href="">Filter by</a>
<ul class="menu float-to-right">
<li class="dropdown"><a href="">Products</a>
<ul class="menu block-element">
<li><a href="">Guestbooks</a></li>
<li><a href="">Notebooks</a></li>
</ul>
</li>
<li class="dropdown block-element"><a href="">Collections</a>
<ul class="menu">
<li><a href="">Handle it</a></li>
<li><a href="">Tuff Love</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Contacts</a></li>
</ul>
<div class="clear"></div>
</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

ViewCheck and every

Can IsInView be set to wait for all elements with the same class, instead of calling them individually? For instance, if there are three sections, can they be called like this: $(".section").on('inview', function(event, isInView) { i ...

Include a horizontal line divider between each row of items that wrap to the next line

I am utilizing CSS flexbox to arrange a variable number of items in rows, wrapping around to additional rows as necessary. My query is, can a horizontal line be inserted between each of the rows? Here you can find a basic example of what I am working on. ...

Displaying country-specific API details (such as capital and currency) in a card container when selecting a country from a dropdown menu

My objective is to display the card information for South Africa as the default value, even before entering any country's name into the search bar input field or selecting a specific country from the provided list. I am utilizing the restcountries API ...

Utilize Ajax and PHP to transfer User ID to the Database

One issue I'm facing is that I have a page where my users' posts are displayed, and each post has a connect button in the form of form input. The poster's id is associated with each post. I want it so that when a user clicks on the connect b ...

Ways to revert a modified string back to its original format in JavaScript

I've been working on a sorting algorithm to intelligently sort filesizes regardless of the input format. The function is designed to sort an array of string inputs to produce the following desired outputs; input = ["5.1 GB","19934563 B& ...

Incorporating external .js files from a different server using node.js

Is it possible to include .js files from another server into a node.js program? I cannot simply use require('./local_file.js') to fetch files over the Internet like this: require('http://www.server.com/path/file.js'); I attempted usin ...

I'm currently utilizing lint in my Angular 2+ project. Is there a way to arrange the constructor parameters in alphabetical order

I am struggling to organize the constructor parameters in TypeScript while using TSLINT with Angular9. I am looking for a rule similar to member-ordering that can help me sort them effectively. constructor( // Sort these private readonly router: R ...

Having trouble making Three.js with instancing work properly unless FrustumCulling is set to false

I've encountered an issue with three.js and instancing, similar to what others have experienced. The objects are randomly clipped and disappear from the camera view. Examples can be found here. Mesh suddenly disappears in three.js. Clipping? Three.j ...

leveraging jQuery's AJAX functionality to send JSON data to a ColdFusion Component

Attempting to implement Jquery/AJAX/JSON with a CFC to send an email has proven to be quite challenging. Despite numerous hours of research, I have not been able to find a comprehensive example that covers the entire process. As I approach the final stages ...

Unable to build a React Native library

For the past month, I've been diligently working on a react native component library, and I believe it's finally in a state where I can publish it as a package on npm. After pushing it to GitHub and running npm publish, everything seemed to be ru ...

Issue with the left margin of the background image

I am currently facing an issue with a series of background images that are supposed to fade in and out behind my content. These images are centered, wider than the content itself, and should not affect the width of the page. However, there is an unexpected ...

Scrollable container with jQuery draggable functionality

I am currently working on implementing a draggable list that I want to contain within a scrollable div. $( ".draggable" ).draggable(); Here is the fiddle I have created for this: http://jsfiddle.net/YvJhE/660/ However, the issue I am facing is that the ...

Changing the position of x-axis labels in a Google Bar Chart to the top

I'm currently working with the Google Bar Chart and I'm trying to relocate the x-axis label to the top. I've tried writing the code, but so far it hasn't been successful. Could you please take a look at the fiddle and screen shot provid ...

Consider using React Components as an alternative to implementing pagination dots within Swiperjs, as it may help resolve the issue displaying

I seem to be encountering a small issue and I believe I am making a mistake somewhere. How can I replace the default dots with custom elements for pagination in Swiperjs for React? The custom element should return an SVG from an array of icons. Initially ...

Issue with mobile functionality: dropdown parent menu link fails to operate

Working on creating a responsive site in Wordpress using Bootstrap. Having trouble with the parent link in the dropdown menu not functioning on mobile devices. Check out the JavaScript snippet below: <script> jQuery(function($) { $('.navb ...

Getting the JavaScript file name within another JavaScript file - a simple guide

Imagine having an HTML file that references two external JavaScript files. One of these JavaScript files contains errors (likely compilation errors), while the other file includes an onerror method without any issues. When you open the HTML file in a brows ...

The SVG element seems to have a mind of its own, refusing to obey my CSS commands and stubbornly remaining centered on the screen. I'm at a loss as

I am facing an issue with positioning SVG elements on a web page. Even though I have set the x,y coordinates to 0,0 and used CSS to position it on the left side of the page, the SVG remains in the center. Here is a link to the problem replicated in jsFidd ...

Refining an array data table within a nested component

Transitioning my old PHP/jquery single-page applications to VueJS/Webpack has been a journey I'm undertaking to familiarize myself with the latter technology. It involves converting a simple table that pulls data from a JSON API and incorporates filte ...

The behavior of my waypoint function becomes unpredictable if the user refreshes the page after scrolling beyond it

I have a navigation bar that changes position to fixed when the user scrolls down past it and back to its original state when scrolling up, using a specific waypoint: var $navbar = $('.navbar-default'); $navbar.waypoint(function(){ if ($(&ap ...

Node.js Express.js Module for Asynchronous SqLite Operations

Currently, I am working on a task that involves making synchronous requests to a database and passing the data to Express. Here is the code snippet: app.get('/', (req, res) => { let db = new sqlite3.Database('./Problems.db'); ...