简体   繁体   English

悬停的项目宽度会扩展,使其适合内部的所有内容

[英]Hovered item width expands so that it can fit all the content inside

I'm building a drop down navigation bar and i have a problem. 我正在构建一个下拉导航栏,但我遇到了问题。 jsfiddle jsfiddle

What I want to achieve is that when the user mouses over a menu item and that item has children items, the navigation container height increases to fit all the content inside it. 我要实现的是,当用户将鼠标悬停在菜单项上并且该菜单项具有子项时,导航容器的高度会增加以适合其中的所有内容。 I managed to achieve that, but the problem is that the hovered item width expands so that it can fit all the content inside. 我设法实现了这一目标,但是问题是,悬停的项目宽度不断扩大,因此可以容纳其中的所有内容。 How can I prevent that? 我该如何预防?

I have this HTML: 我有这个HTML:

<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: CSS:

 * {
    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;
 }

and JS: 和JS:

(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();
    });

Thanks ;) 谢谢 ;)

Like 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 span 宽度向右扩展,但我希望它向左扩展并自动将我的内容放入 span 标签内 - My span width expands to right but I want it to expand left and auto fit my content inside the span tag 当此值更改时,如何确保 Bootstrap 列扩展以适应其内容的宽度? - How can I ensure a Bootstrap column expands to fit the width of its content when this value is changed? 如何压缩内容以使它们适合DIV - how to compress content so that they fit inside DIV 悬停的项目在展开时不会显示隐藏的元素 - Hovered item doesn't show hidden elements when it expands SVG 图表 - 我如何调整 svg 的高度,以便所有标签自动适合内部 - SVG chart - how can i adjust the height of the svg so that all the labels automatically fit inside 输入宽度适合内容 - Input width fit to content 有没有办法让 div 扩展以覆盖所有可用空间,隐藏它扩展的所有内容? - Is there a way I can make a div expand to cover all the available space, hiding all the content it expands onto? 在一定高度和宽度的div中可以容纳多少内容 - how much content can fit in a div of certain height and width DIV内容适合所有屏幕上的“手机背景图像” - DIV content fit inside Mobile Phone Background Image on all screens CSS-使所有内容适合div,中心背景图像 - css - fit all content inside a div, center background image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM