Skip to content

Instantly share code, notes, and snippets.

@jimitit
Created April 24, 2015 13:55
Show Gist options
  • Save jimitit/0338ffb643e88ff9167e to your computer and use it in GitHub Desktop.
Save jimitit/0338ffb643e88ff9167e to your computer and use it in GitHub Desktop.
Make it center
//Make it center e.g $('.spotlight').centerfill();
(function ($, undefined) {
//Changing logic to set left and -ve margin to align center
$.fn.centerfill = function () {
return this.each(function () {
var iWidth = $(this).width();
var iHeight = $(this).height();
var parent = $(this).parent();
//parent.css('position', 'relative');
var pWidth = $(parent).width();
var pHeight = $(parent).height();
var marginTop = 0;
var top = 0;
if (iHeight > pHeight) {
//top = 0;
} else {
top = '50%';
marginTop = 0-iHeight/2
}
var marginLeft = 0;
var left = 0;
if (iWidth > pWidth) {
//left = 0;
} else {
left = '50%';
marginLeft = 0-iWidth/2
}
//alert(marginLeft);
$(this).css({
'top': top,
'margin-top': marginTop,
'left': left,
/*'margin-left': marginLeft*/
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment