Reputation: 8293
I have a container that has products in. But my container is only 200 displaying only a few products. There is a button which increases the height to display all products. Only problem is the height will be changing depending on how many products get added through the CMS. Is it possible to increase the height of the container so it displays all products all of the time (Without hard coding the height)
Fiddle: http://jsfiddle.net/Vx53r/1/
UP DOWN
HTML:
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
<div class="box">Product</div>
</div>
CSS:
.container{
background:grey;
width:500px;
height:200px;
overflow:hidden;
}
.box{
width:120px;
height:220px;
background:pink;
margin:10px;
float:left;
}
JS:
$('#btn-down').click(function(){
$('.container').animate({height:'630px'}, 500);
});
$('#btn-up').click(function(){
$('.container').animate({height: '200px'}, 500);
});
Upvotes: 0
Views: 105
Reputation: 543
You can just use
$('#btn-down').click(function(){
$('.container').animate({height:'auto'}, 500);
});
and remove the
height:200px;
instead so that on the button click it will assume the height to include all items. But this will give you an initial height of full length.
Or else you can use
$('#btn-down').click(function(){
$('.container').animate({height:'100%'}, 500);
});
to start from 200 px
Upvotes: 0
Reputation: 1037
Try with percentage. Working fiddle here
$('#btn-down').click(function(){
$('.container').animate({height:'100%'}, 500);
});
Instead of a fixed pixels you get the whole height, even after adding elements
Upvotes: 0
Reputation: 3678
using the max-height
and toggleClass
css:
.container{
background:grey;
width:500px;
max-height:200px;
overflow:hidden;
}
.box{
width:120px;
height:220px;
background:pink;
margin:10px;
float:left;
}
.boxAutoHeight
{
max-height:none;
}
javascript:
$('#btn-down').click(function(){
$('.container').addClass('boxAutoHeight');
});
$('#btn-up').click(function(){
$('.container').removeClass('boxAutoHeight');
});
demo---->http://jsfiddle.net/junkie/Vx53r/3/
Upvotes: 2
Reputation: 2192
Well you can try this
Next trick is:
Upvotes: 0
Reputation: 27092
You can count the total wrapper height and use that.
$('#btn-down').click(function(){
$('.container').animate({height: Math.ceil($('.box').length / 3) * 240}, 500);
});
$('#btn-up').click(function(){
$('.container').animate({height: '200px'}, 500);
});
Upvotes: 0
Reputation: 337700
To make this dynamic, you need to wrap the .box
elements in another container, like this:
<div class="container">
<div class="box-container">
<div class="box">Product</div>
<!-- more .box -->
</div>
</div>
.box-container {
overflow: hidden;
}
Then in jQuery you can set the height of .container
(which has been truncated) to equal the height of .box-container
, which was not truncated:
$('#btn-down').click(function () {
$('.container').animate({
height: $('.box-container').height()
}, 500);
});
You could create a calculation to work out the number of rows based on 3 items per row, but this way is far easier, and quicker.
Upvotes: 0