Created
April 24, 2015 13:55
-
-
Save jimitit/0338ffb643e88ff9167e to your computer and use it in GitHub Desktop.
Make it center
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
//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