引导程序4: 下拉菜单在屏幕的右边

我好像没法让下拉菜单不从页面上掉下来。我试了几个 BS3的东西,但似乎没有工作。我不知道是不是因为 ml-auto。(忽略 if-else 语句)

这是 CodePen:

<div class="container-fluid">
<nav class="navbar fixed-top navbar-toggleable-md navbar-inverse" style="background-color: #3c763d!important;">


<button class="navbar-toggler navbar-toggler-right navbar-toggler-custom" type="button" data-toggle="collapse" data-target="#searchNav" aria-controls="searchav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>


</button>


<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>


</button>


<!-- <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarToggleExternalContent" aria-controls="navbarToggleExternalContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button> -->


<div id="logo">
<a class="navbar-brand" href="/">Company</a>
</div>


<div class="collapse navbar-collapse" id="searchNav">
<ul class="navbar-nav mx-auto">


<form action="/search" class="form-inline">
<input class="form-control mr-sm-2" id="keyword" name="keyword" :value='keyword' type="text" placeholder="Search for jobs" />
<!-- <div class="input-group"> -->


<input class="form-control mr-sm-2" id="location" name="location" :value='location' type="text" placeholder="Location" />
<!-- <button class="btn btn-secondary" type="button">Find Jobs</button> -->
<!-- </span> -->
<!-- </div> -->


<button class="btn btn-secondary my-2 my-sm-0" type="submit">Find Jobs</button>
</form>


</ul>
</div>
<div class="collapse navbar-collapse" id="navbarNav">


<ul class="navbar-nav ml-auto">
<li class="nav-item"><a href="/post" class="nav-link"><strong>Post Job</strong></a></li>


<div class="hidden-lg-up">


<li v-if="!user_logged" class="nav-item">
<a v-if="!user_logged" href="/signup" class="nav-link"><strong>Register</strong></a>
</li>


<li v-if="user_logged && user_type === 'user'" class="nav-item">
<a v-if="user_logged && user_type === 'user'" href="/profile" class="nav-link"><strong>Profile</strong></a>
</li>


<li v-if="user_logged && user_type === 'user'" class="nav-item">
<a v-if="user_logged && user_type === 'user'" href="/applied" class="nav-link"><strong>My Jobs</strong></a>
</li>


<li v-if="user_logged && user_type === 'user'" class="nav-item">
<a v-if="user_logged && user_type === 'user'" href="/searches" class="nav-link"><strong>My Searches</strong></a>
</li>


<li v-if="user_logged && user_type === 'company'" class="nav-item">
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="nav-link"><strong>Employer Dashboard</strong></a>
</li>


<li v-if="!user_logged" class="nav-item">
<a v-if="!user_logged" href="/login" class="nav-link"><strong>Login</strong></a>
</li>


<li v-if="user_logged" class="nav-item">
<a v-if="user_logged" href="/logout" class="nav-link"><strong>Logout</strong></a>
</li>
</div>


<div class="hidden-md-down">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="/" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">


<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/signup" class="dropdown-item"><strong>Register</strong></a>
<!-- </li> -->


<!-- <li v-if="user_logged && user_type === 'user'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'user'" href="/profile" class="dropdown-item"><strong>Profile</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/applied" class="dropdown-item"><strong>My Jobs</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/searches" class="dropdown-item"><strong>My Searches</strong></a>


<!-- </li> -->


<!-- <li v-if="user_logged && user_type === 'company'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="dropdown-item"><strong>Employer Dashboard</strong></a>
<!-- </li> -->


<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/login" class="dropdown-item"><strong>Login</strong></a>
<!-- </li> -->


<!-- <li v-if="user_logged" class="nav-item"> -->
<a v-if="user_logged" href="/logout" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
<!-- </li> -->
</div>


</li>
</div>


</ul>
</div>
</nav>


</div>
128318 次浏览

在样式表中,对于 .dropdown-menu,将 left: 0;更改为 right: 0;

Bootstrap 已经内置了这个功能: 参见 菜单对齐。只需将 dropdown-menu-right类添加到 dropdown-menu div。

<div class="dropdown-menu dropdown-menu-right">

工作示例: (打开完整页面)

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-xl navbar-dark bg-primary">


<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>


<a class="navbar-brand" href="#">Company</a>


<div class="collapse navbar-collapse" id="navbarSupportedContent">
<form action="#" class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" id="keyword" name="keyword" :value='keyword' type="text" placeholder="Search for jobs" />
<input class="form-control mr-sm-2" id="location" name="location" :value='location' type="text" placeholder="Location" />
<button class="btn btn-secondary my-2 my-sm-0" type="submit">Find Jobs</button>
</form>


<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a href="#" class="nav-link active">
<strong>Post Job</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Register</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Profile</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>My Jobs</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>My Searches</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Employer Dashboard</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Login</strong>
</a>
</li>
<li class="nav-item">
<a href="#" class="nav-link">
<strong>Logout</strong>
</a>
</li>
</ul>


<ul class="navbar-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item">
<strong>Register</strong>
</a>
<a href="#" class="dropdown-item">
<strong>Profile</strong>
</a>
<a href="#" class="dropdown-item">
<strong>My Jobs</strong>
</a>
<a href="#" class="dropdown-item">
<strong>My Searches</strong>
</a>
<a href="#" class="dropdown-item">
<strong>Employer Dashboard</strong>
</a>
<a href="#" class="dropdown-item">
<strong>Login</strong>
</a>
<a href="#" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
</div>
</li>
</ul>
</div>
</nav>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.4.2/umd/popper.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>

对于那些在谷歌搜索时偶然发现这个问题但正在使用反应的人来说,接受的答案(尽管是正确的)不会为你解决这个问题。手动添加到 className 时,React 会忽略“下拉-菜单-右边”。相反,将代码更新到下面,它将正确地设置相同的类。

对于 javascript

<DropdownMenu right>

打字稿

<DropdownMenu right={true}>

把它放在这里,因为这个线程出现时,我正在谷歌的问题,并花了我相当一段时间,以找出正确的解决方案引导4在反应(反作用)。

我看了你的代码,我认为公认的答案解决了一个不同的问题。如果菜单移动到页面的左边或右边,而且不在按钮下面,那是因为您错过了父类 .btn-group,而且似乎您的 .dropdown-toggle.dropdown-menu类位于错误的元素上。下面是 Bootstrap 4文档中最基本的示例。

<div class="btn-group">
<button type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Right-aligned menu
</button>
<div class="dropdown-menu dropdown-menu-right">
<button class="dropdown-item" type="button">Action</button>
<button class="dropdown-item" type="button">Another action</button>
<button class="dropdown-item" type="button">Something else here</button>
</div>
</div>

我只浏览了一小段代码,但是在大屏幕上有一个下拉菜单的最后,我把重点放在了这里。

 <li class="nav-item dropdown"> <!--- change this line --->
<a class="nav-link dropdown-toggle" href="/" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">


<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/signup" class="dropdown-item"><strong>Register</strong></a>
<!-- </li> -->


<!-- <li v-if="user_logged && user_type === 'user'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'user'" href="/profile" class="dropdown-item"><strong>Profile</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/applied" class="dropdown-item"><strong>My Jobs</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/searches" class="dropdown-item"><strong>My Searches</strong></a>


<!-- </li> -->


<!-- <li v-if="user_logged && user_type === 'company'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="dropdown-item"><strong>Employer Dashboard</strong></a>
<!-- </li> -->


<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/login" class="dropdown-item"><strong>Login</strong></a>
<!-- </li> -->


<!-- <li v-if="user_logged" class="nav-item"> -->
<a v-if="user_logged" href="/logout" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
<!-- </li> -->
</div>


</li>

 <li class="nav-item btn-group">
<a class="nav-link dropdown-toggle" href="/" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<strong>Account</strong>
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">


<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/signup" class="dropdown-item"><strong>Register</strong></a>
<!-- </li> -->


<!-- <li v-if="user_logged && user_type === 'user'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'user'" href="/profile" class="dropdown-item"><strong>Profile</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/applied" class="dropdown-item"><strong>My Jobs</strong></a>
<a v-if="user_logged && user_type === 'user'" href="/searches" class="dropdown-item"><strong>My Searches</strong></a>


<!-- </li> -->


<!-- <li v-if="user_logged && user_type === 'company'" class="nav-item"> -->
<a v-if="user_logged && user_type === 'company'" href="/dashboard" class="dropdown-item"><strong>Employer Dashboard</strong></a>
<!-- </li> -->


<!-- <li v-if="!user_logged" class="nav-item"> -->
<a v-if="!user_logged" href="/login" class="dropdown-item"><strong>Login</strong></a>
<!-- </li> -->


<!-- <li v-if="user_logged" class="nav-item"> -->
<a v-if="user_logged" href="/logout" class="dropdown-item">
<div class="dropdown-divider"></div>
<strong>Logout</strong>
</a>
<!-- </li> -->
</div>


</li>

在 Bootstrap 5(beta 2)中,你可以使用 dropdown-menu-end:

<div class="dropdown-menu dropdown-menu-end">
...
</div>