Skip to content

Instantly share code, notes, and snippets.

@klauskpm
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klauskpm/fc25fef2b4c94131d854 to your computer and use it in GitHub Desktop.
Save klauskpm/fc25fef2b4c94131d854 to your computer and use it in GitHub Desktop.
Plugin to centralize the element according to the window
// jQuery Plugin - Centralize
//
// Needs:
// - jQuery(2.1.1) >> http://code.jquery.com/jquery-2.1.1.js
(function($) {
/**
* Plugin to centralize the element according to the window
* @param {boolean} resize If true, it will resize everytime the window
* resizes
* @returns {jQuery.this}
*/
$.fn.centralize = function(resize) {
this.css({
top: ((window.innerHeight - this.outerHeight()) / 2) + 'px',
left: ((window.innerWidth - this.outerWidth()) / 2) + 'px'
});
if(resize) {
var element = this;
$(window).resize(function() {
element.centralize();
});
}
return this;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment