Bootstrap下拉菜单添加水平滚动

时间:2018-09-24 11:48:34

标签: bootstrap-4 popper.js

使用bootstrap 4和popper.js,每当我向navbar-right添加一个下拉列表并单击该按钮时,该下拉列表便会向页面添加水平滚动,您必须向右滚动才能看到整个下拉列表。

如何解决此问题,以便在显示下拉菜单时不进行水平滚动?

这是我使用的代码示例:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />
        <title id="title">Title</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    </head> 
    <body>
        <nav class="navbar navbar-expand-sm navbar-light">
            <a class="navbar-brand" href="/home/">
                Brand
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarex1" aria-controls="navbarex1" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav mr-auto navbarex1">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 1</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 2</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 3</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 4</a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <div class="dropdown show">
                        <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Dropdown link
                        </a>
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                            <a class="dropdown-item" href="#">Action</a>
                            <a class="dropdown-item" href="#">Another action</a>
                            <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                    </div>
                </ul>
            </div>
        </nav>            
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

我已经覆盖了引导程序的默认设置,以将下拉菜单的最小宽度设置为0,并将下拉菜单项的空白设置为正常。这样可以使文本很好地环绕并且不会溢出正文。

.dropdown-menu{min-width:0;}
.dropdown-menu .dropdown-item{white-space:normal;}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="initial-scale = 1.0,maximum-scale = 1.0" />
        <title id="title">Title</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    </head> 
    <body>
        <nav class="navbar navbar-expand-sm navbar-light">
            <a class="navbar-brand" href="/home/">
                Brand
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarex1" aria-controls="navbarex1" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse">
                <ul class="navbar-nav mr-auto navbarex1">
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 1</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 2</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 3</a>
                    </li>
                    <li class="nav-item active">
                        <a class="nav-link" href="#">Link 4</a>
                    </li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <div class="dropdown show">
                        <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Dropdown link
                        </a>
                        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                            <a class="dropdown-item" href="#">Action</a>
                            <a class="dropdown-item" href="#">Another action</a>
                            <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                    </div>
                </ul>
            </div>
        </nav>            
    </body>
</html>

答案 1 :(得分:0)

overflow: scroll-y属性添加到您的身体

body {
    overflow: scroll-y;
    overflow-x: hidden;
}

这样,除非指定,否则任何元素都不会溢出正文的宽度,但仍允许垂直滚动