Created
September 17, 2012 22:10
-
-
Save dorey/3740092 to your computer and use it in GitHub Desktop.
appending an hr into the page whenever an element exceeds a page limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// dpi.get() code is from stackoverflow: | |
// http://stackoverflow.com/questions/2252030/how-can-i-find-out-a-web-page-viewers-pixels-per-inch#answer-2312609 | |
var dpi = { | |
v: 0, | |
get: function (noCache) { | |
if (noCache || dpi.v == 0) { | |
e = document.body.appendChild(document.createElement('DIV')); | |
e.style.width = '1in'; | |
e.style.padding = '0'; | |
dpi.v = e.offsetWidth; | |
e.parentNode.removeChild(e); | |
} | |
return dpi.v; | |
} | |
}; | |
function addInPageBreaks(){ | |
var pageHeightInInches = 10, | |
latestBreak = 0, | |
pageHeightInPixels; //? | |
pageHeightInPixels = dpi.get() * pageHeightInInches; | |
function nextBreak(){ return latestBreak + pageHeightInPixels; } | |
function insertBreakBefore(elem) { | |
var hr = $("<hr>", {'class':'page-break'}); | |
elem.before(hr); | |
latestBreak = hr.offset().top; | |
} | |
$('*').each(function(){ | |
var $this = $(this), | |
$tot = $this.offset().top; | |
if($tot > nextBreak()) { | |
insertBreakBefore($this.parents('fieldset').eq(0)); | |
} | |
}); | |
}; | |
$(addInPageBreaks); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment