Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created May 26, 2014 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonjs/759bc36a990c2cee5763 to your computer and use it in GitHub Desktop.
Save samsonjs/759bc36a990c2cee5763 to your computer and use it in GitHub Desktop.
Sometimes you just gotta centre this bitch.
// Courtesy of http://www.chupamobile.com
function center_this_bitch(target, context, animate) {
var top, left, context_target;
animate = typeof animate !== 'undefined' ? animate : false;
switch (context) {
case'parent':
context_target = target.parent();
top = ((context_target.height() - target.height()) / 2) + 'px';
left = ((context_target.width() - target.width()) / 2) + 'px';
break;
case'window':
top = ((user['window-height'] - target.height()) / 2) + 'px';
left = ((user['window-width'] - target.width()) / 2) + 'px';
break;
default:
top = ((context.height()) / 2) + 'px';
left = ((context.width()) / 2) + 'px';
break;
}
if (animate == 'animate') {
target.animate({
'top': top,
'left': left
});
} else {
target.css({
'top': top,
'left': left
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment