if (options.transition > 0) {$(this).animate(props, options.transition, callback);} else {$(this).css(props);if (typeof callback == 'function') { // make sure the callback is a functioncallback.call(this); // brings the scope to the callback}}
// We add a pos parameter so we can specify which position type we want
// Center it both horizontally and vertically (dead center)jQuery.fn.center = function (pos) {this.css("position", pos);this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));return this;}
// Center it horizontally onlyjQuery.fn.centerHor = function (pos) {this.css("position", pos);this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));return this;}
// Center it vertically onlyjQuery.fn.centerVer = function (pos) {this.css("position", pos);this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));return this;}
$.fn.center = function () {this.css("position","absolute");
//use % so that modal window will adjust with browser resizingthis.css("top","50%");this.css("left","50%");
//use negative margin to centerthis.css("margin-left",(-1*this.outerWidth()/2)+($(window).scrollLeft())+"px");this.css("margin-top",(-1*this.outerHeight()/2)+($(window).scrollTop())+"px");
//catch cases where object would be offscreenif(this.offset().top<0)this.css({"top":"5px","margin-top":"0"});if(this.offset().left<0)this.css({"left":"5px","margin-left":"0"});
return this;};
$(window).resize(function(){$('.className').css({position:'absolute',left: ($(window).width() - $('.className').outerWidth())/2,top: ($(window).height() - $('.className').outerHeight())/2});});
// To initially run the function:$(window).resize();
body {margin : 0;height:100vh;width:100%;background: #ccc;}#main{background:#00cc00;width:100px;height:100px;}body.center{display:flex;align-items:center;justify-content:center;}