简体   繁体   English

如果导航栏的高度固定,为什么引导下拉菜单不起作用?

[英]Why bootstrap dropdown doesn't work if navbar has fixed height?

I have a bootstrap navigation menu with more than 20 items and I want to get a fixed height to navigation therefore the menus that exceed the width of the container do not breaks but are hidden. 我有一个自举导航菜单,其中包含20多个项目,并且我希望导航具有固定的高度,因此,超出容器宽度的菜单不会中断,而是被隐藏了。

ul.fixedHeight {
    height: 50px;
    overflow: hidden;
}

Then I checked with jQuery if there is a hidden menu to show the button.navbar-toggle to slidedown and show the hidden menu: 然后我用jQuery检查是否有一个隐藏的菜单来显示button.navbar-toggle向下滑动并显示隐藏的菜单:

JS: JS:

function showhidebtn(){
    var element = document.querySelector('ul.navbar-nav');
    if((element.offsetHeight < element.scrollHeight) || (element.offsetWidth < element.scrollWidth)){
        $("button.navbar-toggle").removeClass("hidden");
        $("button.navbar-toggle").addClass("visible");
    } else { 
        $("button.navbar-toggle").removeClass("visible");
        $("button.navbar-toggle").addClass("hidden"); }
} 

CSS: CSS:

@media (min-width: 768px) {
    button.navbar-toggle {
        position: absolute;
        bottom: 0;
        margin-left: -50px;
        background: #000;
    }
    button.navbar-toggle.hidden {
        display:none;
    }
    button.navbar-toggle.visible {
        display:block;
    }
}

Lastly I run the function if the window size is greater than 768 or or when it is resized. 最后,如果窗口大小大于768或调整窗口大小时,我将运行该函数。 jsFiddle demo jsFiddle演示

The problem is that when window size is greater than 768 and I click to the button to show the hidden items the slidedown doesn't work, but it works when window size is less than 768. 问题是,当窗口大小大于768时,我单击该按钮以显示隐藏的项目,则下拉菜单不起作用,但是当窗口大小小于768时它起作用。

Any help is appreciated! 任何帮助表示赞赏! Thank you! 谢谢!

As said, the height is restricting it from growing when it's supposed to on the button click. 如前所述,该高度限制了它在应该单击按钮时的增长。 However, you could fix this with javascript: 但是,您可以使用javascript修复此问题:

//initially the button is not clicked
clicked=false;
$('button.navbar-toggle').click(function(){
    if(clicked==false)
    {
        //if the button isn't clicked and you click it,
        //let the height grow and make the overflow property as visible
        $('.nav.navbar-nav.fixedHeight').css('overflow','visible');
        $('.nav.navbar-nav.fixedHeight').css('height','auto');
        clicked=true;
    }
    else
    {
       //vice versa when you need to close it
       $('.nav.navbar-nav.fixedHeight').css('overflow','hidden');
       $('.nav.navbar-nav.fixedHeight').css('height','50px');
        clicked=false;
    }

});

DEMO DEMO

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM