Created
January 12, 2015 08:22
-
-
Save tillsanders/767070ea6c40c06d3e90 to your computer and use it in GitHub Desktop.
Center position with jQuery
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
$.fn.centerPosition = function() { | |
"use strict"; | |
var $targets = $(this); | |
if (!$targets.length) { | |
$targets = $('.centered:visible'); | |
} | |
$targets.each(function() { | |
$(this).css('margin-top', 0); | |
$(this).css('margin-top', Math.max(0, | |
Math.floor(($(this).parent().height() - $(this).outerHeight()) / 2) | |
)); | |
}); | |
return $targets; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment