js auto center element

var frm = $('');
   var modal=$('<div class="modal"></div>');
   $(modal).css({width: '100%', height:2000,top:0, left:0,position:'absolute','z-index': '998',background: '#999','opacity' :'0.8'});
   $(modal).click(function (){
    $(frm).remove();
    $(this).remove();
   });
   var h,w;
   h = (cb_widget_config.height) ? cb_widget_config.height:300;
   w = (cb_widget_config.width) ? cb_widget_config.width:500;
   frm.css({'height':h, 'width':w, 'position':'fixed','z-index': '999'});
   
   $('body').append(modal);
   $('body').append(frm);
   var auto_center = function(e){
    var w=$(window).width();
    var h=$(window).height();
    $(e).css({
     'left':(w-$(e).width())/2,
     'top':(h-$(e).height())/2,
    });
   }
   auto_center(frm);
   window.onresize= function (){auto_center(frm);}

Bài đăng phổ biến từ blog này

Đệ quy trong MIPS