jquery - 处理 Twitter Bootstrap 下拉列表在容器上自动溢出的问题

标签 jquery css twitter-bootstrap

Bootstrap 下拉菜单被剪裁,我查看了以下问题中提到的所有选项,但没有用。

Bootstrap drop down cutting off

Twitter Bootstrap Button dropdown z-index layering issue

z-index issue with twitter bootstrap dropdown menu

要求

  • 我想在主 div 中滚动
  • 我希望 bootstrap 下拉列表不被剪裁
  • div 和下拉列表的高度宽度不应更改

笨蛋: http://plnkr.co/edit/HOKKYJ?p=preview

<!DOCTYPE html>
<html>

  <head>
    <title>Clipping problem</title>
        <link data-require="bootstrap@*" data-semver="3.3.5" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css" />
        <link data-require="bootstrap@*" data-semver="3.3.5" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
        <script data-require="jquery@*" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
        <script data-require="bootstrap@*" data-semver="3.3.5" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
     <script>
            angular.module("myApp", []).controller("MainCtrl", function($scope) {
            });
        </script>
  </head>

 <body style="height:100%;" ng-app="myApp">
        <div style="background: indianred; position: relative; left:40%; height: 200px; width: 250px; overflow-y: auto;">
                <div ng-repeat="num in [1,2,3,4,5]">
                    <div style="background: bisque; margin:5px; height: 50px;">
                        <div style="position: relative; float: right">
                            <button class="dropdown-toggle" data-toggle="dropdown">Click me Brother...!!</button>
                            <div class="dropdown-menu">
                                <div style="text-wrap: none; white-space: nowrap;">I am a text......................................!!</div>
                                <div style="text-wrap: none;white-space: nowrap;">I am a also text.................................!!</div>
                                <div style="text-wrap: none;white-space: nowrap;">Another text........................................................................................!!</div>
                                <div style="text-wrap: none;white-space: nowrap;">One more text.............................!!</div>
                                <div style="text-wrap: none;white-space: nowrap;">Aha one more text...........................................................................!!</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    </body>

</html>

有问题:

Current situation

我想要什么:

This is what i want

提前致谢

最佳答案

问题

我使用的是 Bootstrap 3,在使用自动溢出的 div 容器时遇到了同样的问题。它包含一个必须从容器中弹出的下拉菜单。

解决方案

解决方案是设置 position:fixed 到下拉菜单,然后在点击时使用 jQuery 计算坐标。坐标是用 offset() 计算的,因此它是相对于文档窗口而不是父元素的。

这个解决方案可能也适用于 overflow:hidden 容器。

CSS

.somecontainer .dropdown-menu {
    position:fixed;
}

jQuery

$(".somecontainer .dropdown").on('click', function() {
    $(this).find('.dropdown-menu').css('top',$(this).offset().top);
    $(this).find('.dropdown-menu').css('left',$(this).offset().left);
});

可选地添加这个以隐藏窗口调整大小的下拉列表:

$( window ).resize(function() {         
    $('.somecontainer .dropdown-menu').parent().removeClass('open');
}); 

关于jquery - 处理 Twitter Bootstrap 下拉列表在容器上自动溢出的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32381746/

相关文章:

javascript - 查明某个 id 本身是否包含特定文本部分

html - 如何将表格样式设置为看起来像 gridview

javascript - html按钮在没有被禁用的情况下不可点击

html - Bootstrap 3 中的内联输入问题

javascript - 如何使用 touchmove 使 mousemove 事件适用于触摸屏?

javascript - CloudZoom 与 Fancybox

javascript - 如何在 jquery mobile 中显示之前设置页面中选择框的值?

css - 自动组织或按字母顺序排列 CSS 属性的最佳工具是什么?

javascript - 如何更改 Bootstrap 响应式?

javascript - 进度条寻找正确的目标元素