javascript - How do I center a div in the middle of the page using jQuery and keep it centered when window is re-sized? -


does know of jquery script keep div centered in middle , when window re-sized, keep centered? building lightbox plugin , need stay in middle. tried using css doesn't work.

also need stay in middle when box opened has bigger width , height. here's page examples on it:

http://wowbox.yolasite.com/

open image first, , open div, , you'll see mean. div not centered. when close , re-open div centered because of .center() happens when click on wowbox link. need centered, , never mess that.

here code i'm using center it:

  jquery.fn.center = function () {     this.css("position","absolute");     this.css("top", (($(window).height() - this.outerheight()) / 2) + $(window).scrolltop() + "px");     this.css("left", (($(window).width() - this.outerwidth()) / 2) + $(window).scrollleft() + "px");     return this;   } 

i want make when window re-sizes, stay centered. want make when width of box gets wider , height gets taller, re-center. right doesn't work right.

could this?

thanks in advance,

nathan

try along example code have. clarification:

jquery.fn.center = function () {     this.css("position","absolute");     this.css("top", (($(window).height() - this.outerheight()) / 2) + $(window).scrolltop() + "px");     this.css("left", (($(window).width() - this.outerwidth()) / 2) + $(window).scrollleft() + "px");     return this; }  $(document).ready(function(){    $('.element').center();    window.onresize = function(event) {         $('.element').center();     } }); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -