Skip to content

Instantly share code, notes, and snippets.

@Langmans
Created March 29, 2019 09:30
Show Gist options
  • Save Langmans/039dce586889e899fea6ff87790bc9c0 to your computer and use it in GitHub Desktop.
Save Langmans/039dce586889e899fea6ff87790bc9c0 to your computer and use it in GitHub Desktop.
if (typeof assert !== 'function') {
function assert(expr, message) {
if (!Boolean(expr)) {
throw new Error(message || 'unknown assertion error');
}
}
}
function elementScreenQuadrant(element) {
var type = typeof (element);
assert(type === 'object', 'element must be an object, got ' + type);
assert(element instanceof HTMLElement, 'element must be instance of HTMLElement, got ' + element.constructor.name);
assert(typeof element.getBoundingClientRect === 'function', 'element does not have getBoundingClientRect');
var rect = element.getBoundingClientRect();
var top = rect.top,
left = rect.left,
right = window.innerWidth - rect.right,
bottom = window.innerHeight - rect.bottom;
return (top > bottom ? 'bottom' : 'top') + (left > right ? 'right' : 'left');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment