我在使用引导程序4时遇到问题(具体来说,是reactstrap,但是我可以在引导程序中重新创建相同的问题),其中我正在使用“卡片栏”创建卡片网格,每个卡片在“卡片”中都有一个下拉菜单-页脚”。 问题是,一旦我拥有4张或更多张卡片,下拉菜单就会以某种方式横穿各列。...呈现的位置与chrome突出显示元素的位置不同,如下图所示:
如果我增加卡数,那么在某些卡上,当我打开下拉菜单时,卡会改变位置-它们“跳来跳去”。
最小示例:
body {
margin: 10px;
}
.dropdown-menu {
margin-top: 10px;
margin-bottom: 10px;
}
<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>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container-fluid h-100">
<div class="mx-4 my-2 row">
<div class="mx-4 row">
<div class="card-columns">
<div class="card border-secondary">
<div class="card-footer">
<div class="row">
<div class="col-md-8">
<div class="btn-group show">
<button class="dropdown-toggle btn btn-outline-secondary">
More
</button>
</div>
</div>
</div>
</div>
</div>
<div class="card border-secondary">
<div class="card-footer">
<div class="row">
<div class="col-md-8">
<div class="btn-group show">
<button class="dropdown-toggle btn btn-outline-secondary">
More
</button>
</div>
</div>
</div>
</div>
</div>
<div class="card border-secondary">
<div class="card-footer">
<div class="row">
<div class="col-md-8">
<div class="btn-group show">
<button class="dropdown-toggle btn btn-outline-secondary">
More
</button>
</div>
</div>
</div>
</div>
</div>
<div class="card border-secondary">
<div class="card-footer">
<div class="row">
<div class="col-md-8">
<div class="btn-group show">
<button class="dropdown-toggle btn btn-outline-secondary">
More
</button>
<div role="menu" class="dropdown-menu show" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 27px, 0px);" data-placement="bottom-start">
<button class="dropdown-item">
Button 1
</button>
<button class="dropdown-item">
Button 2
</button>
<button class="dropdown-item">
Button 3
</button>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="card border-secondary">
<div class="card-footer">
<div class="row">
<div class="col-md-8">
<div class="btn-group show">
<button class="dropdown-toggle btn btn-outline-secondary">
More
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
在此示例中,菜单的最后2个按钮似乎“断开”并出现在其他位置-您的屏幕必须足够宽,以显示多于1列的内容才能发生问题。
我想问题出在打开的地方,即使下拉菜单的位置绝对正确,下拉菜单也会超出其边界。
如何在不干扰卡片列布局的情况下进行此类下拉?
答案 0 :(得分:2)
其原因似乎是card-columns
。这些列是使用CSS列创建的,并更改了其中的内容显示,例如下拉菜单似乎会使他们跳到各处。
我为您的代码创建了一个更简单的版本来尝试证明这一点。 在版本1中,当您单击下拉列表切换时,列会跳来跳去。 我添加了一个带注释的CSS块,将其覆盖以证明这是原因。如果您取消评论它有效(使用flexbox)。
因此,我建议您不要使用card-column
,而应使用Bootstrap网格。
虽然这不会给您砌石的效果,但是比您得到的怪异效果要好。
/** Click that version 1 dropdowns.
The layout jumps around.
I think that's because of the logic behind the scenes of CSS columns.
Uncomment the below to prove that columns is the cause.
Don't actually do it in your code.
Instead, consider using the bootstrap grid instead of card columns.
**/
/* .card-columns {
column-count: auto !important;
display: flex;
flex-wrap: wrap;
}
.card-columns > * {
flex: 0 1 33.3%;
} */
<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>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<h2>Version 1 - CSS Columns</h2>
<div class="card-columns">
<div class="card">
<div class="row">
<div class="col-md-8">
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Small button
</button>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="row">
<div class="col-md-8">
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Small button
</button>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Small button
</button>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
</div>
</div>
</div>
</div>
<h2>Version 2 - Grid</h2>
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="row">
<div class="col-md-8">
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Small button
</button>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="row">
<div class="col-md-8">
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Small button
</button>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="row">
<div class="col-md-8">
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Small button
</button>
<div class="dropdown-menu">
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
<a href="#" class="dropdown-item">item</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>