以下代碼塊就能始終讓彈出div位於屏幕的正中心,希望對大家有所幫助!
. 代碼如下:
//讓指定的DIV始終顯示在屏幕正中間
function setDivCenter(divName){
var top = ($(window).height() - $(divName).height())/2;
var left = ($(window).width() - $(divName).width())/2;
var scrollTop = $(document).scrollTop();
var scrollLeft = $(document).scrollLeft();
$(divName).css( { position : 'absolute', 'top' : top + scrollTop, left : left + scrollLeft } ).show();
}