Skip to content

Instantly share code, notes, and snippets.

@josephwegner
Created July 27, 2012 14:58
Show Gist options
  • Save josephwegner/3188502 to your computer and use it in GitHub Desktop.
Save josephwegner/3188502 to your computer and use it in GitHub Desktop.
Visual Scoring of Main Content via Javascript
/*
* Really all this does so far is give a score based on how much the element protrudes off the center. Center content is usually the focus
*/
function getOffset( el ) {
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
return { top: _y, left: _x };
}
function score(doc) {
var left = getOffset(doc).left;
var right = doc.offsetWidth + left;
var mid = document.width / 2;
return ((mid - left) < (right - mid)) ? (mid - left) : (right - mid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment