Last active
August 29, 2015 14:09
-
-
Save klauskpm/fc25fef2b4c94131d854 to your computer and use it in GitHub Desktop.
Plugin to centralize the element according to the window
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
// 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