本篇文章主要是對jquery彈出層居中效果的簡單實例進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助
jquery實現彈出層完美居中效果 代碼如下: showDiv($("#pop")); function showDiv(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 }); }