DIV CSS 佈局教程網

JS 彈出視窗在任意解析度下居中的實作
編輯:JavaScript綜合知識     

彈窗要真正「居中」,不能只算視窗寬高——還得把捲動條的位移量加回去,否則頁面捲到中段時彈窗就會跑到看不見的地方。本文示範居中、居右、居左三種定位的完整做法。

計算公式

水平位置:(視窗寬 - 彈窗寬) / 2 + 水平捲動量
垂直位置:(視窗高 - 彈窗高) / 2 + 垂直捲動量

加上捲動量,是因為 position:absolute 的定位基準是文件而非視窗,不補回位移就會失準。

HTML 與事件綁定

<input type="button" value="彈出居中的窗口" id="btn_center">
<input type="button" value="彈出居右的窗口" id="btn_right">
<input type="button" value="彈出居左的窗口" id="btn_left">
<div class="window" id="center">
<div class="title"><img src="close.gif">csdn歡迎您</div>
<div class="content">哈哈哈哈哈哈哈</div>
</div>

<script>
$(document).ready(function (){
 $("#btn_center").click(function (){
  $(window).scroll(function (){
   popcenterWindow();
  });
 });
 $("#btn_right").click(function (){
  $(window).scroll(function (){
   poprightWindow();
  });
 });
 $("#btn_left").click(function (){
  $(window).scroll(function (){
   popleftWindow();
  });
 });
});
</script>

JS 定位函式

//窗口的高度、寬度、彈窗高度寬度、捲動量
var windowHeight;
var windowWidth;
var popHeight;
var popWidth;
var scrollTop;
var scrollleft;
var timeout;
function init(){
 windowHeight=$(window).height();
 windowWidth=$(window).width();
 popHeight=$(".window").height();
 popWidht=$(".window").width();
 scrollTop=$(window).scrollTop();
 scrollleft=$(window).scrollLeft();
}
function closeWindow(){
 $(".title img").click(function (){
  $(this).parent().parent().hide("slow");
 });
}
function popcenterWindow(){
 clearTimeout(timeout);
 timeout=setTimeout(function (){
  init();
  var popY=(windowHeight-popHeight)/2+scrollTop;
  var popX=(windowWidth-popWidht)/2+scrollleft;
  $("#center").animate({top:popY,left:popX},300).show("slow");
 },300);
 closeWindow();
}
function popleftWindow(){
 clearTimeout(timeout);
 timeout=setTimeout(function (){
  init();
  var popY=windowHeight+scrollTop-popHeight-10;
  var popX=scrollleft-5;
  $("#left").animate({top:popY,left:popX},300).show("slow");
 },300);
 closeWindow();
}
function poprightWindow(){
 clearTimeout(timeout);
 timeout=setTimeout(function (){
  init();
  var popY=windowHeight-popHeight+scrollTop-10;
  var popX=windowWidth-popWidht+scrollleft-10;
  $("#right").animate({top:popY,left:popX},300).show("slow");
 },300);
 closeWindow();
}

CSS

.window{
 width:250px;
 background-color:#3FF;
 padding:2px;
 margin:5px;
 position:absolute;
 display:none;
}
.content{
 height:150px;
 background-color:#FFF;
 padding:2px;
 font-size:14px;
 overflow:auto;
}
.title{
 padding:2px;
 color:#999;
 font-size:14px;
}
.title img{
 float:right;
 cursor:pointer;
}

更省事、也更可靠的做法

如果目標只是「永遠置中」,用 position:fixed 就不需要任何捲動計算,瀏覽器會自動以視窗為基準:

.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

搭配 Flexbox 的 display:flex; align-items:center; justify-content:center 覆蓋層更是經典解法。另外範例把 $(window).scroll() 綁在每次點擊裡,會隨點擊次數不斷堆疊監聽器,是常見效能陷阱——事件綁定應該只執行一次。若彈窗需要遮罩、鍵盤 ESC 關閉與焦點鎖定,建議直接採用 <dialog> 元素或成熟彈窗元件,這些無障礙細節自己實作很容易漏。

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