Created
March 12, 2011 01:00
-
-
Save danro/866885 to your computer and use it in GitHub Desktop.
jQuery vertical align function
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
(function ($) { | |
// vertical align (parent relative) | |
$.fn.vAlign = function() { | |
return this.each(function(i) { | |
var ah = $(this).height(); | |
var ph = $(this).parent().height(); | |
var mh = Math.ceil((ph-ah) / 2); | |
$(this).css('margin-top', mh); | |
}); | |
}; | |
// vertical align (window relative) | |
$.fn.vAlignWin = function() { | |
return this.each(function(i) { | |
var ah = $(this).height(); | |
var ph = $(window).height(); | |
var mh = (ph - ah) / 2; | |
if(mh>0) { | |
$(this).css('margin-top', mh); | |
} else { | |
$(this).css('margin-top', 0); | |
} | |
}); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment