Created
December 11, 2015 17:46
-
-
Save sardbaba/a4f2b86d937533781f0e to your computer and use it in GitHub Desktop.
jQuery plugin for centering vertically and/or horizontally
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( $ ) { | |
$.fn.vcenter = function () { | |
this.css("position","absolute"); | |
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px"); | |
return this; | |
} | |
$.fn.hcenter = function () { | |
this.css("position","absolute"); | |
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px"); | |
return this; | |
} | |
}( jQuery )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
$('#myelement').vcenter();