I made a version of how it would work, in a very simple way (put it all over):
var DH = $(document).height();
$(window).on('resize', function() {
DH = $(document).height();
})
$('.add').click(function() {
var topbarH = $('.topBar').height();
var footerH = $('.footer').height();
var exemploH = 0;
$('.exemplo').each(function(i) {
exemploH += +$(this).innerHeight() + parseFloat($(this).css('margin'));
})
if (exemploH <= DH - topbarH - footerH - 40) {
$('.container').append('<div class="exemplo">Aqui tem uma div</div>')
}
})
* {
margin: 0;
padding: 0;
border: 0;
box-sizing: border-box;
}
.container {
position: relative;
width: 100%;
height: calc(100vh - 120px);
top: 60px;
}
.exemplo {
width: 100%;
height: 20px;
margin: 10px 0;
background: #333;
color: #fff;
}
.topBar {
width: 100%;
height: 60px;
background: #ccc;
position: fixed;
top: 0;
}
.footer {
width: 100%;
height: 60px;
background: #ccc;
position: fixed;
bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="topBar">
<button class="add" style="padding: 10px">Adicionar</button>
</div>
<div class="container"></div>
<div class="footer"></div>
Only elements are excluded when the page is resized.
If you do not want to block creation you can create the scroll bar, with overflow-y: auto
in div.container
:
$('.add').click(function() {
$('.container').append('<div class="exemplo">Aqui tem uma div</div>')
})
* {
margin: 0;
padding: 0;
border: 0;
box-sizing: border-box;
}
.container {
position: relative;
width: 100%;
height: calc(100vh - 120px);
top: 60px;
overflow-y: auto;
}
.exemplo {
width: 100%;
height: 20px;
margin: 10px 0;
background: #333;
color: #fff;
}
.topBar {
width: 100%;
height: 60px;
background: #ccc;
position: fixed;
top: 0;
}
.footer {
width: 100%;
height: 60px;
background: #ccc;
position: fixed;
bottom: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="topBar">
<button class="add" style="padding: 10px">Adicionar</button>
</div>
<div class="container"></div>
<div class="footer"></div>