问题
It's my very first question here and i hope you can help me. I'm trying to set up a product-overview page with the bootstrap grid. I have got 4 products in a row for large screen sizes. For smaller screen sizes i'd like to have 2 products per row.
The trick is, i want to have the product details in a collapsed container between the rows, that opens up, when i click on a product. It's working for the large screen sizes with 4 products. But how to achieve that the collapsed container is showing up right after the row, where i clicked my product, independent of how many products i have got per row. On smaller sizes the collapsed container opens after the second row, even if i click a product in the first row.
When i place every collapsed container right behind the product in the markup it pushes away the other products when i open it up.
Sorry, it's a bit complicated, i hope you know what i mean.
For better understanding have a look on my pen on codepen.
I want to keep it as simple as possible and I'm not sure which way to go?
http://codepen.io/auftakt/pen/PWxJVX
Tanks to all.
$('.collapse-btn').on('click',function(){$('.collapse').collapse('hide');})
body {
margin-top: 50px;
overflow-y: scroll;
}
.img-responsive {
width:100%;
}
.col-xs-6{
margin-bottom: 20px;
}
.product-detail {
width: 100%;
height:300px;
background-size: cover;
background-position: center center;
}
.no-padding-left {
padding-left: 0;
}
.product-collapse-wrap > div {
margin-bottom: 20px;
}
<div class="container">
<div class="row">
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-1" aria-expanded="false" aria-controls="Product-1">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-2" aria-expanded="false" aria-controls="Product-2">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-3" aria-expanded="false" aria-controls="Product-3">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-4" aria-expanded="false" aria-controls="Product-4">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
</div>
<div class="product-collapse-wrap">
<div id="Product-1" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 1</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
<div id="Product-2" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 2</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
<div id="Product-3" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 3</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
<div id="Product-4" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 4</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-5" aria-expanded="false" aria-controls="Product-5">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-6" aria-expanded="false" aria-controls="Product-6">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-7" aria-expanded="false" aria-controls="Product-7">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
<div class="col-md-3 col-xs-6">
<a class="collapse-btn" data-toggle="collapse" href="#Product-8" aria-expanded="false" aria-controls="Product-8">
<img class="img-responsive" src="http://placehold.it/600x600">
</a>
</div>
</div>
<div class="product-collapse-wrap">
<div id="Product-5" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 5</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
<div id="Product-6" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 6</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
<div id="Product-7" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 7</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
<div id="Product-8" class="col-md-12 collapse">
<div class="row">
<div class="col-md-6 no-padding-left">
<div class="img-responsive product-detail" style="background-image: url('http://placehold.it/800x600')"></div>
</div>
<div class="col-md-6">
<h3>Product 8</h3>
<ul>
<li>Flachheizkörper</li>
<li>Planheizkörper</li>
</ul>
</div>
</div>
</div>
</div>
回答1:
A while back, I answered a similar question here: Bootstrap responsive grid layout (3 columns) with collapsed/hidden col-12 beneath each grid element
Basically you have to put the collapsed rows below the appropriate col-*
to fit your smallest layout. So in your case:
<div class="row">
<div class="col-md-3 col-xs-6">
<a href="#one" data-toggle="collapse"><img src="//placehold.it/600"></a>
</div>
<div class="col-md-3 col-xs-6">
<a href="#two" data-toggle="collapse"><img src="//placehold.it/600"></a>
</div>
<div class="col-xs-12 overlay collapse" id="one">
1
</div>
<div class="col-xs-12 overlay collapse" id="two">
2
</div>
<div class="col-md-3 col-xs-6">
<a href="#three" data-toggle="collapse"><img src="//placehold.it/600"></a>
</div>
<div class="col-md-3 col-xs-6">
<a href="#four" data-toggle="collapse"><img src="//placehold.it/600"></a>
</div>
<div class="col-xs-12 overlay collapse" id="three">
3
</div>
<div class="col-xs-12 overlay collapse" id="four">
4
</div>
</div>
And then you'd need some extra CSS to overlay
the collapse column so that it's full width under the entire row on larger screens.
Demo: http://www.codeply.com/go/0QGguzIYCx
For Bootstrap 4, this scenario is easier because of flexbox ordering..
http://codeply.com/go/qdUGPVL4HJ (alpha 6)
http://codeply.com/go/TLJi5MxQ1E (Bootstrap 4.0.0)
http://codeply.com/go/EsRR1nGa36 (Bootstrap 4.3.0)
来源:https://stackoverflow.com/questions/42166401/bootstrap-grid-with-collapsed-container-in-between