Skip to content

Instantly share code, notes, and snippets.

@mohitkh7
Created May 4, 2019 18:06
Show Gist options
  • Save mohitkh7/ae813544a845826e21d3bc3a6ff1c2e6 to your computer and use it in GitHub Desktop.
Save mohitkh7/ae813544a845826e21d3bc3a6ff1c2e6 to your computer and use it in GitHub Desktop.
A scroll able div inside collapsible panel having variable height
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<style>
#scroll-section{
position: relative;
height: 350px;
overflow: auto;
display: block;
}
</style>
</head>
<body>
<div class="container">
<h2>Collapsible Panel</h2>
<p>Click on the collapsible panel to open and close it.</p>
<!-- Panel 1 -->
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse1">Panel 1</a>
</h4>
</div>
<div id="collapse1" class="panel-collapse collapse">
<div class="panel-body">Panel Body</div>
<div class="panel-footer">Panel Footer</div>
</div>
</div>
</div>
<!-- Panel 2 -->
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse2">Panel 2</a>
</h4>
</div>
<div id="collapse2" class="panel-collapse collapse">
<div class="panel-body" id="scroll-section">
<table class="table table-bordered" id="table1">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>[email protected]</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>[email protected]</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>[email protected]</td>
</tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
<tr><td>July</td><td>Dooley</td><td>[email protected]</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Panel 3 -->
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collapse3">Panel 3</a>
</h4>
</div>
<div id="collapse3" class="panel-collapse collapse">
<div class="panel-body">Panel Body</div>
<div class="panel-footer">Panel Footer</div>
</div>
</div>
</div>
</div>
</body>
<script>
function change_height(h) {
// increaments or decrements height of below element
var el = document.getElementById("scroll-section");
var height = el.offsetHeight;
var newHeight = height + h;
el.style.height = newHeight + 'px';
}
// On opening & Closing Panel 1 take action
$('#collapse1').on('shown.bs.collapse', function () {
console.log("Opened");
change_height(-100);
});
$('#collapse1').on('hidden.bs.collapse', function () {
console.log("Closed");
change_height(100);
});
// On opening & Closing Panel 3 take action
$('#collapse3').on('shown.bs.collapse', function () {
console.log("Opened");
// document.getElementById("scroll-section").style.height = "200px";
change_height(-100);
});
$('#collapse3').on('hidden.bs.collapse', function () {
console.log("Closed");
change_height(100);
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment