js 控制div 居中

来源:互联网 发布:egress软件 编辑:程序博客网 时间:2024/05/18 00:50
function center(obj){
var windowWidth = document.documentElement.clientWidth;   
var windowHeight = document.documentElement.clientHeight;   
var popupHeight = $(obj).height();   
var popupWidth = $(obj).width();    
$(obj).css({   
 "position": "absolute",   
 "top": (windowHeight-popupHeight)/2+$(document).scrollTop(),   
 "left": (windowWidth-popupWidth)/2   
});  

}


//滚动的时候自动居中

$(window).scroll(function(){

center(obj);

});


//窗口变化自动居中

$(window).resize(function(){

center(obj);

});


//按回车提交
$(document).keydown(function(e){
  if(e.keyCode ==13){
  //要调用的方法
}
});








0 0
原创粉丝点击