Looking for create my very first Jquery Plugin. Basically center vertically a div in a blank page.
(function($){
$.fn.centerdiv = function(options) {
var defaults = {
ratio : 2
};
var options = $.extend(defaults, options);
// get some useful variables
var $this = $(this);
var $heightDiv = $this.height();
var $heightWindow = $(window).height();
var $marginTop = Math.round(($heightWindow - $heightDiv)/options.ratio);
var applyMargin = function() {
$this.css('margin-top',$marginTop+'px');
console.log($heightWindow);
}
$(window).resize(applyMargin);
applyMargin();
return $this;
}; // fn.centerdiv
})(jQuery);
It works but not for the "window resize". Found another answer ( jQuery $(window).resize not firing within jQuery plugin ) but adding "window" after $ does not solve my problem. How i can have this plugin working also with window resize? Thank you to all!
Edited with answer...