I'm creating a script for a printable page that's generated dynamically. Because this page is generated on the fly and the sections could have varying heights, a predetermined page-break class inserted in the markup doesn't cut it. To get around this, I'm checking the height of each with a data-print="section" attribute.
What I'm struggling with is... each data-print="section" needs to generate a running total. When that total is greater than the variable "pageHeight" it inserts to force the content to a second page. Once a page-break is inserted, it needs to start the running total over again and insert another when the total is greater than "pageHeight".
Example before "script" with break at 960px...
<div class="item" data-print="section"></div> = 400px
<div class="item" data-print="section"></div> = 200px
<ul data-print="section"> = 300px
<li>
<li>
</ul>
<div class="item" data-print="section"></div> = 50px
<div class="item" data-print="section"></div> = 100px
Example after "script" with break at 960px...
<div class="item" data-print="section"></div> = 400px
<div class="item" data-print="section"></div> = 200px
<ul data-print="section"> = 300px
<li>
<li>
</ul>
<div class="item" data-print="section"></div> = 50px
<div class="page-break"></div>
<div class="item" data-print="section"></div> = 100px
Current Script
$('[data-print=section]').each(function() {
var staticHeight = $(this).filter(':visible').outerHeight(true);
var pageHeight = 400
console.log(staticHeight)
if ($(this).height() > pageHeight) {
$(this).after( '<div class="page-break"></div>');
}
});
Thanks in advance