DIV CSS 佈局教程網

用 Ajax 載入外部頁面做出彈跳層效果
編輯:AJAX基礎知識     

這個範例的趣味之處在於:先用純 CSS 的寬高變化做出一個「由小放大」的開場動畫,等動畫結束才真正送出 Ajax 請求把外部頁面的內容填進來。視覺上是彈跳層,技術上則是 XHR 加定時器。

完整程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax加載外部頁面的一個彈出層效果</title>
<style>
body { margin:0px }
#Loading {
  position:absolute; z-index:10; left:10px; top:10px;
  border:1px #666666 solid; background:#eeeeee; width:10px; height:10px;
}
.LoadContent { width:100%; height:100%; overflow:auto }
</style>

<script LANGUAGE="JavaScript">
<!--
function $(){
  return document.getElementById ? document.getElementById(arguments[0]) : eval(arguments[0]);
}

var OverH, OverW, ChangeDesc, ChangeH = 50, ChangeW = 50;

function OpenDiv(_Dw, _Dh, _Desc) {
  $("Loading").innerHTML = "Loading...";
  OverH = _Dh; OverW = _Dw; ChangeDesc = _Desc;
  $("Loading").style.display = '';

  // 依長寬比決定每次放大的步長
  if (_Dw > _Dh) {
    ChangeH = Math.ceil((_Dh - 10) / ((_Dw - 10) / 50));
  } else if (_Dw < _Dh) {
    ChangeW = Math.ceil((_Dw - 10) / ((_Dh - 10) / 50));
  }

  $("Loading").style.top  = (document.documentElement.clientHeight - 10) / 2 + "px";
  $("Loading").style.left = (document.documentElement.clientWidth  - 10) / 2 + "px";

  OpenNow();
}

var Nw = 10, Nh = 10;

function OpenNow() {
  if (Nw > OverW - ChangeW) ChangeW = 2;
  if (Nh > OverH - ChangeH) ChangeH = 2;

  Nw = Nw + ChangeW;
  Nh = Nh + ChangeH;

  if (OverW > Nw || OverH > Nh) {
    if (OverW > Nw) {
      $("Loading").style.width = Nw + "px";
      $("Loading").style.left  = (document.documentElement.clientWidth - Nw) / 2 + "px";
    }
    if (OverH > Nh) {
      $("Loading").style.height = Nh + "px";
      $("Loading").style.top    = (document.documentElement.clientHeight - Nh) / 2 + "px";
    }
    window.setTimeout("OpenNow()", 10);
  } else {
    // 動畫結束,才真正去抓內容
    Nw = 10; Nh = 10; ChangeH = 50; ChangeW = 50;
    AjaxGet(ChangeDesc);
  }
}

// 建立 XML 物件
function createXMLHttps(){
  var ret = null;
  try {
    ret = new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {
    try {
      ret = new ActiveXObject('Microsoft.XMLHTTP');
    } catch (ee) {
      ret = null;
    }
  }
  if (!ret && typeof XMLHttpRequest != 'undefined') ret = new XMLHttpRequest();
  return ret;
}

function AjaxGet(URL) {
  var xmlhttp = createXMLHttps();
  xmlhttp.open("Get", URL, true);
  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 404) {
      $("Loading").innerHTML = '讀取頁面失敗,文件' + URL + '不存在!';
      return;
    }
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      $("Loading").innerHTML = "<div class='LoadContent'>" + xmlhttp.responseText + "</div>";
    }
  };
  xmlhttp.send(null);
}
//-->
</script>
</head>

<body>
<a href="javascript:OpenDiv(500,300,'index.html')">首頁 500*300</a><br><br>
<a href="javascript:OpenDiv(500,200,'index.html')">Test 500*200</a><br><br>
<a href="javascript:OpenDiv(200,500,'l.html')">子頁面 200*500</a><br><br>

<div id="Loading" style="display:none" ondblclick="this.style.display='none'"></div>

注意:需加載的文件必須在子目錄下,也就是本文件需處於上級才行。頁面編碼 UTF8。
</body>
</html>

幾點觀察

OpenNow 用 setTimeout 每秒約一百次的方式逐步放大,並在接近目標尺寸時把步長收斂為 2,避免衝過頭。內容則刻意等到動畫結尾才透過 AjaxGet 載入——這個順序讓使用者先看到容器成形,再看到內容填入,比反過來自然。

若要改寫成現代版本,我會做三件事:用 position: fixed 取代 absolute 並手動計算居中座標;用 CSS transition 取代 setTimeout 迴圈做尺寸動畫,效能與流暢度都好得多;再補上一層半透明遮罩與 ESC 關閉,讓彈跳層的行為符合使用者預期。

另外要提醒:把外部 HTML 直接塞進 innerHTML,其中的 script 不會被執行,但 style 與 class 名稱卻可能與主頁面衝突。若載入的來源不完全可控,使用前應先評估這層風險。

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