DIV CSS 佈局教程網

jQuery 實用函式:讓彈出層完美居中的程式碼
編輯:關於JavaScript     

本文分享一段 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
});
}

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved