jquery弹出层完美居中
showDiv($("#pop"));
function showDiv(obj){
$(obj).show();
center(obj);
$(window).scroll(function(){
center(obj);
});
$(window).resize(function(){
center(obj);
});
}
$(obj).show();
center(obj);
$(window).scroll(function(){
center(obj);
});
$(window).resize(function(){
center(obj);
});
}
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
});
}
本文介绍了一个使用jQuery实现的弹出层自动居中的方法。通过监听窗口滚动和调整大小事件,确保弹出层始终处于屏幕中央位置。适用于各种屏幕尺寸。
1363

被折叠的 条评论
为什么被折叠?



