我似乎无法将下拉项目移到页面上。我尝试了BS3的一些东西,但它们似乎不起作用。我不确定是不是因为ml-auto。 (忽略if-else语句)
继承人 http://codepen.io/bbennett36/pen/oByzgw
<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>
答案 0 :(得分:123)
Bootstrap已内置此功能:请参阅Menu Alignment。只需将dropdown-menu-right
类添加到dropdown-menu
div。
<div class="dropdown-menu dropdown-menu-right">
工作示例:
.navbar {
background-color: #3c763d;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid">
<nav class="navbar fixed-top navbar-toggleable-md navbar-inverse">
<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>
<div id="logo">
<a class="navbar-brand" href="#">Company</a>
</div>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav mr-auto hidden-md-down">
<form action="#" class="form-inline">
<input class="form-control" id="keyword" name="keyword" :value='keyword' type="text" placeholder="Search for jobs" />
<input class="form-control" id="location" name="location" :value='location' type="text" placeholder="Location" />
<button class="btn btn-secondary" type="submit">Find Jobs</button>
</form>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item"><a href="#" class="nav-link"><strong>Post Job</strong></a>
</li>
<div class="hidden-lg-up">
<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>
</div>
<li class="nav-item dropdown hidden-md-down">
<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 dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink">
<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>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
答案 1 :(得分:6)
在您的样式表中,.dropdown-menu
将left: 0;
更改为right: 0;
。
答案 2 :(得分:3)
为了使那些在谷歌搜索时偶然发现此问题但正在使用反应的人受益,公认的答案(虽然正确)不会为您解决问题。当您手动将其添加到className时,React会忽略“ dropdown-menu-right”。相反,将您的代码更新到下面,即可正确设置相同的类。
对于javascript
<DropdownMenu right>
对于打字稿
<DropdownMenu right={true}>
将其放入此处是因为在我搜索该问题时出现了该线程,花了我相当长的时间才能找出反应(reactstrap)中引导程序4的正确解决方案。
答案 3 :(得分:2)
在 Bootstrap 5(测试版 2)中,您可以使用 dropdown-menu-end
:
<div class="dropdown-menu dropdown-menu-end">
...
</div>
答案 4 :(得分:1)
我正在查看您的代码,我认为已接受的答案解决了另一个问题。如果菜单移动到页面的左侧或右侧而不是按钮的下方,那是因为您缺少父类.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>