简体   繁体   English

隐藏在其他元素后面的 Bootstrap 下拉菜单

[英]Bootstrap dropdown menu hidden behind other elements

I have a problem similar to this .我有一个类似的问题 But the solutions there does not work for my issue.但是那里的解决方案不适用于我的问题。 And that discussion has been closed for further suggestions.该讨论已结束以征求进一步的建议。 Unfortunately I can't show my code exactly as it is on jsfiddle.不幸的是,我无法完全按照在 jsfiddle 上显示我的代码。 However the issue is something like this .但是问题是这样的。 When trying to expand the dropdown inside first panel section, the dropdown hidden behind other elements.尝试展开第一个面板部分内的下拉菜单时,下拉菜单隐藏在其他元素后面。 I've spent hours trying different suggestions concerning "stacking context", "position", and "z-index".我花了几个小时尝试关于“堆叠上下文”、“位置”和“z-index”的不同建议。 I would really appreciate if anyone could point me to any resources that could help.如果有人能指出任何可以提供帮助的资源,我将不胜感激。

<div class="btn-group">
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> <span data-bind="label">Select One</span>&nbsp;<span class="caret"></span>

                    </button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">Item 1</a>
                        </li>
                        <li><a href="#">Another item</a>
                        </li>
                        <li><a href="#">This is a longer item that will not fit properly</a>
                        </li>
                    </ul>
                </div>

You have two options. 你有两个选择。 You can make the container's overflow expand to fit the content by setting 'overflow: auto' to the parent's css. 您可以通过将'overflow:auto'设置为父级的css来扩展容器的溢出以适应内容。

FIDDLE DEMO FIDDLE DEMO

.panel-body { /* parent container of the menu */
    overflow:auto;
}

Or you could do something functionally with JavaScript to handle the menu placement. 或者您可以使用JavaScript在功能上执行某些操作来处理菜单放置。 This option is a little bit 'hacky' and would require further improvements. 这个选项有点'hacky',需要进一步改进。 You would need to improve it to handle multiple menus open at once on the same page, and you would need to improve how it selects the menu. 您需要改进它以在同一页面上同时打开多个菜单,并且您需要改进它选择菜单的方式。 You might also want to add scroll support to move the menu with its target element unless that's not an issue. 您可能还希望添加滚动支持以使用其目标元素移动菜单,除非这不是问题。 Here's the fiddle: 这是小提琴:

FIDDLE DEMO FIDDLE DEMO

(function() {
  // hold onto the drop down menu                                             
  var dropdownMenu;

  // and when you show it, move it to the body                                     
  $(window).on('show.bs.dropdown', function(e) {

    // grab the menu        
    dropdownMenu = $(e.target).find('.dropdown-menu');

    // detach it and append it to the body
    $('body').append(dropdownMenu.detach());   

    // grab the new offset position
    var eOffset = $(e.target).offset();

    // make sure to place it where it would normally go (this could be improved)
    dropdownMenu.css({
        'display': 'block',
        'top': eOffset.top + $(e.target).outerHeight(),
        'left': eOffset.left
    });                                                
  });

  // and when you hide it, reattach the drop down, and hide it normally                                                   
  $(window).on('hide.bs.dropdown', function(e) {        
    $(e.target).append(dropdownMenu.detach());        
    dropdownMenu.hide();                              
  });                                                   
})(); 

There is probably a better way to do this, but the concept is to move the menu from the confines of the parent element to the body. 可能有更好的方法,但概念是将菜单从父元素的范围移动到正文。 When you're relying on the html data-attributes to setup the menu, you'll have trouble doing this, but that is where the second solution I reference becomes useful. 当您依靠html数据属性来设置菜单时,您将无法执行此操作,但这是我引用的第二个解决方案变得有用的地方。

Sadly I would have suggested looking into the 'Via Javascript' options that bootstrap provides, but apparently they don't have the option to set the target append element. 遗憾的是,我建议查看bootstrap提供的“Via Javascript”选项,但显然他们没有设置目标append元素的选项。 It would have been nice to have an option to append the menu on to whichever element you want like most of their other items: Bootstrap Docs 很高兴有一个选项可以将菜单附加到你想要的任何元素上,就像其他大多数项目一样: Bootstrap Docs


EDIT 编辑

As @Andrea Pizzirani mentioned, you could try removing the css for the menu absolute positioning, but I wouldn't recommend it! 正如@Andrea Pizzirani所提到的,你可以尝试删除菜单绝对定位的css,但我不推荐它! That will mess up all other instances of the menu unless you add other css to restrict scope of the change. 这会搞乱菜单的所有其他实例,除非您添加其他css来限制更改的范围。 Also, if you want the menu positioned under the button, you'll have to redo CSS that bootstrap already had in place. 此外,如果您希望菜单位于按钮下方,则必须重做已经存在的引导程序的CSS。

Still, if it works, and you're not worried about messing up all other menus, it may be the solution you were looking for. 尽管如此,如果它有效,并且您不担心搞乱所有其他菜单,它可能是您正在寻找的解决方案。 Heres a fiddle demonstrating the solution: 这是一个演示解决方案的小提琴:

FIDDLE DEMO FIDDLE DEMO

dropdown-menu {}

EDIT I finally found where I originally found this solution. 编辑我终于找到了我最初找到这个解决方案的地方。 Gotta give credit where credit is due! 信用到期时必须给予信任

Try to remove position: absolute; 尝试删除位置:绝对; from: 从:

dropdown-menu {}

and place the menu under the button by css. 并通过css将菜单放在按钮下。

Modification of Jonathan's answer above, which was a huge help, but I modified it to place the menu back at it's source row, which allows custom jQuery to function properly (because the menu still exists at its initial parent element hierarchy. My use case was a jQuery DataTable with frozen columns. We had a bootstrap 4 "Action" dropdown-menu embedded in a cell of that table. The menu was appearing behind the hidden columns. This function allowed it be copied and pasted above the fixed columns where users can actually interact with it.修改上面乔纳森的回答,这是一个巨大的帮助,但我修改了它以将菜单放回它的源行,这允许自定义 jQuery 正常运行(因为菜单仍然存在于其初始父元素层次结构中。我的用例是带有冻结列的 jQuery 数据表。我们在该表的单元格中嵌入了一个引导程序 4“操作”下拉菜单。菜单出现在隐藏列的后面。此功能允许将其复制并粘贴到用户可以使用的固定列上方实际上与之交互。

function BringActionMenuToForeground() {
    var dropdownMenu;

    /// When you show the menu, remove it from its current row and place it right back. This puts it on top of the DOM.
    $(window).on('show.bs.dropdown', function (e) {
        /// grab the menu
        dropdownMenu = $(e.target).find('.dropdown-menu');
        /// get the row where the menu appers.
        var menuRow = $(dropdownMenu).closest("tr");
        /// detach it and append it right back to the row from which it was taken.
        $(menuRow).append(dropdownMenu.detach());

        /// grab the new offset position
        var eOffset = $(e.target).offset();

        /// make sure to place it where it would normally go (this could be improved)
        dropdownMenu.css({
            'display': 'block',
            'top': eOffset.top + $(e.target).outerHeight(),
            'left': eOffset.left
        });
    });

    /// and when you hide it, reattach the drop down, and hide it normally                                                   
    $(window).on('hide.bs.dropdown', function (e) {
        $(e.target).append(dropdownMenu.detach());
        dropdownMenu.hide();
    });
}

Add an additional class on your HTML and use the below codepen reference code.在您的 HTML 上添加一个额外的 class 并使用下面的代码笔参考代码。

check here: https://codepen.io/qpqinc/pen/yLyPVMJ

You need to add only: 您只需要添加:

.panel.panel-default{
      overflow: inherit !important;
    }

JSFIDDLE DEMO JSFIDDLE DEMO

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

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