Reputation: 495
I'm using the Gridstack.js framework in an app and have some div columns:
<div class="col-lg-6 s_panel">
<div class="cont">
<!-- 1st div content -->
</div>
</div>
<div class="col-lg-6 s_panel">
<div class="grid-stack">
<!-- Gridstack content -->
</div>
</div>
And I'm setting the height of the 1st Div to be the same as whatever the Gridstack container is like this:
$(document).ready(function () {
var divHeight = $('.grid-stack').css("height");
$('.cont').css('height', divHeight);
});
So on load the divs are both the same height, but if the Gridstack div has more items added to it and grows in height I wanted the 1st div to also be the same height.
Is there a way to dynamically make the 1st div change height to be the same as the Gridstack div as its adjusted?
Upvotes: 1
Views: 4438
Reputation: 1207
You can achieve this simply using css
without nay jquery
or javascript
.
Use css flex
properties.
.s_panel_container {
display: flex;
}
.s_panel {
flex: 1;
}
.cont {
background-color: pink;
}
.grid-stack {
background-color: #ccfff5;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="s_panel_container">
<div class="col-lg-6 s_panel cont">
1st div content
</div>
<div class="col-lg-6 s_panel grid-stack">
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</div>
</div>
Upvotes: 0
Reputation: 2373
In case you're using ajax call, I would suggest you should create a function of height change instead of $(document).ready
like so:
var helpers = {
heightChange() {
var divHeight = $('.grid-stack').css("height");
$('.cont').css('height', divHeight);
}
};
And then in your ajax call to makes change/put data in .grid-stack
you only need to call your function, full code for example:
<script type="text/javascript">
var helpers = {
heightChange() {
var divHeight = $('.grid-stack').css("height");
$('.cont').css('height', divHeight);
}
};
$.ajax({
url: "test.php",
type: "post",
data: values ,
success: function (response) {
$('.grid-stack').append(response);
//call height change here
helpers.heightChange();
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
</script>
Hope this help, feel free to tell me your other problem.
Upvotes: 2
Reputation: 12181
This might help you https://jsfiddle.net/4uqd3jqL/
$(document).ready(function () {
$('.cont').css('height', $('.grid-stack').css("height"));
setTimeout(function(){
$('.grid-stack').css("height", $(this).height() + 100);
$('.cont').css('height', $('.grid-stack').css("height"));
}, 1000);
});
.grid-stack{
height:300px;
background: red;
}
.cont{
height:200px;
background: blue;
}
.col-lg-6{
width:50%;
display: inline-block;
float:left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-6 s_panel">
<div class="cont">
<!-- 1st div content -->
</div>
</div>
<div class="col-lg-6 s_panel">
<div class="grid-stack">
<!-- Gridstack content -->
</div>
</div>
Since I don't have any Ajax call, I have used setTimeout.
In setTimeout I'm increasing the height of grid-stack container by 100px & then setting the height of cont by height of grid-stack
Upvotes: 0