Skip to content

Instantly share code, notes, and snippets.

@tillsanders
Created January 12, 2015 08:22
Show Gist options
  • Save tillsanders/767070ea6c40c06d3e90 to your computer and use it in GitHub Desktop.
Save tillsanders/767070ea6c40c06d3e90 to your computer and use it in GitHub Desktop.
Center position with jQuery
$.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