javascript - center on resize too -
this works:
$.fn.center = function () { this.css("position", "absolute"); this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrolltop() + "px"); this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollleft() + "px"); return }; $('#container').center(); .. element stays in same position if window resized, how center resize too?
thanks
you need execute code in window resize event. but, if browser resized event fires huge! it's in general idea create little "balancer".
$(window).bind('resize', function() { var = this; if(!('balancer' in that) ) { that.balancer = settimeout(function() { $('#container').center(); delete that.balancer; }, 200); } }); this absorb many events fired when resizing browser window. in fact, calls .center() @ maximum of 200ms. should increase value , cache node reference #container element.
Comments
Post a Comment