核心代码:
复制代码 代码如下:
//获取窗口的高度
var windowHeight;
//获取窗口的宽度
var windowWidth;
//获取弹窗的宽度
var popWidth;
//获取弹窗高度
var popHeight;
function init(){
windowHeight=$(window).height();
windowWidth=$(window).width();
popHeight=$(".window").height();
popWidth=$(".window").width();
}
//关闭窗口的方法
function closeWindow(){
$(".title img").click(function(){
$(this).parent().parent().hide("slow");
});
}
//定义弹出居中窗口的方法
function popCenterWindow(){
init();
//计算弹出窗口的左上角Y的偏移量
var popY=(windowHeight-popHeight)/2;
var popX=(windowWidth-popWidth)/2;
//alert('/jihua);
//设定窗口的位置
$("#right").css("top",popY-50).css("left",popX-50).slideToggle("slow");
closeWindow();
}
完整代码:
jQuery弹出窗口 - .window{ width:250px; background-color:#d0def0; position:absolute; padding:2px; margin:5px; display:none; } .content{ height:150px; background-color:#FFF; font-size:14px; overflow:auto; } .title{ padding:2px; color:#0CF; font-size:14px; } .title img{ float:right; } -居中窗口 www.jb51.net -居左窗口 www.jb51.net -居右窗口 www.jb51.net [Ctrl+A 全选 注:引入外部Js需再刷新一下页面才能执行]
作者 cnblogs jihua
