DIV CSS 佈局教程網

同時兼容 IE 與 Firefox 的 iframe onload 遮罩層方案
編輯:關於JavaScript     

頁面中嵌入 iframe 載入較耗時的內容時,使用者往往盯著空白區域不知道發生什麼事。理想的做法是先把一層「處理中」的遮罩蓋上去,等 iframe 觸發 onload 再把它收掉。聽起來簡單,但 IE 與 Firefox 對動態插入的 iframe 事件綁定方式不同,需要多繞一點路。

關鍵做法

示範不直接寫死 <iframe>,而是在使用者點擊按鈕時,用字串拼出 iframe 並寫入容器,把 onLoad="removeDiv()" 一起嵌進標籤裡。這個技巧繞過了 IE 對動態建立節點時 onload 綁定不敏感的問題,因為事件屬性在 HTML 被解析的當下就已存在。遮罩層本身是一個動態 createElement 出來的 div,設為絕對定位、寬度 100%、z-index 拉高,並同時寫入 opacity 與 IE 專用的 filter: Alpha 以兼顧兩邊的透明度實作。

另外示範中還有一個小細節:點擊後先把按鈕 disabled 並換成等待中的圖示,等 removeDiv() 被呼叫時再呼叫 Timer() 復原,避免使用者重複觸發請求。

 <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title></title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> <!-- #Layer1 { position: absolute; width: 896px; height: 472px; z-index: 1; left: 97px; top: 120px; } #Layer2 { position: absolute; width: 127px; height: 86px; z-index: 2; left: 1159px; top: 240px; } #Layer3 { position: absolute; width: 200px; height: 115px; z-index: 3; left: 404px; top: 160px; } body { background-image: url(lib/images/index111.jpg); margin-left: -10px; } .input_image{border:0px solid #808080; background-color:#006699;} --> </style> </head> <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false" onmouseup="document.selection.empty()" > <div id="Layer3"> <img src="lib/images/welcome33.gif" /> </div> <div id="Layer1"> <iframe src="earth.jsp" id="Layer1"></iframe> </div> <div id="Layer2"> <input class="input_image" type=image src="lib/images/website.jpg" id="website_earth" alt="點擊按鈕查詢站點分布地圖"/> <br><br> <input class="input_image" type=image src="lib/images/associator.jpg" id="select_ass" alt="點擊按鈕查詢用戶卡信息"/> <br><br> <input class="input_image" type=image src="lib/images/yichang.jpg" id="yichang" alt="點擊按鈕處理異常卡信息"/> <br><br> <input class="input_image" type=image src="lib/images/lvchang.jpg" id="gtbike" alt="點擊查看公司簡介"/> </div> </body> </html> <script> var secs=20; var wait=secs*100; //切換按下時的圖片 function timing(){ document.getElementById("select_ass").disabled =true; document.getElementById("select_ass").src = "lib/images/123123.jpg"; //定時切換圖片 /*for(i=1;i<=(wait/100);i++) { window.setTimeout("doUpdate(" + i + ")", i * 100); } window.setTimeout("Timer()", wait); } function doUpdate(num) { if(num == (wait/100)) { document.getElementById("select_ass").src = "lib/images/associator.jpg"; } else { wut = (wait/100)-num; document.getElementById("select_ass").src = "lib/images/123123.jpg"; }*/ } //切換原始圖片 function Timer(){ document.getElementById("select_ass").disabled =false; document.getElementById("select_ass").src = "lib/images/associator.jpg"; } var btMax =document.getElementById("Layer1"); website_earth.onclick= function(){ displayDiv(); btMax.innerHTML ="<iframe src=earth.jsp id=Layer1 onLoad='removeDiv()'></iframe>"; } select_ass.onclick= function(){ displayDiv(); timing(); btMax.innerHTML ="<iframe src=Client?name=1 id=Layer1 onLoad='removeDiv()'></iframe>"; //js創建iframe對象 /*var NewFrame=window.parent.document.createElement("iframe"); NewFrame.id="Layer1"; NewFrame.setAttribute("frameBorder","0"); NewFrame.setAttribute("scrolling","no"); NewFrame.setAttribute("onLoad","removeDiv()"); NewFrame.setAttribute("src","Client?name=1"); btMax.appendChild(NewFrame);*/ } yichang.onclick= function(){ displayDiv(); btMax.innerHTML ="<iframe src=Client?name=2 id=Layer1 onLoad='removeDiv()'></iframe>"; } gtbike.onclick= function(){ btMax.innerHTML ="<iframe src=lib/images/61111.jpg id=Layer1></iframe>"; } //遮罩層顯示 var mybg; displayDiv=function() { mybg = document.createElement("div"); mybg.setAttribute("id","mybg"); mybg.innerHTML="<center>正在處理中, 請稍候<img src='lib/images/loading.gif'></img></center>"; mybg.style.width = "100%"; mybg.style.position = "absolute";//絕對位置顯示 mybg.style.top = "260"; mybg.style.left = "0"; mybg.style.zIndex = "100";//z軸位置 mybg.style.opacity = "0.8";//透明度 mybg.style.filter = "Alpha(opacity=80)";//濾鏡顯示透明度 document.body.appendChild(mybg); } //遮罩層移除 removeDiv=function() { document.body.removeChild(mybg); Timer(); } </script> 

如果今天不必再管舊版 IE

這份程式寫在還要遷就 IE 的年代,所以有幾處是歷史包袱:透明度要寫兩次、document.all 之類的判斷、以及用 innerHTML 拼 iframe。現代的等價寫法乾淨得多——用 document.createElement('iframe') 建立節點後直接 iframe.onload = removeDiv,遮罩層用一層寫死在 CSS 裡的 position: fixed 元素切換 class 即可,完全不需要動態新增移除。

想更進一步的話,可以替遮罩加上 pointer-events 隔離互動、為載入失敗準備逾時降級,並用 aria-busy 讓螢幕閱讀器知道內容正在載入。這些都是原範例沒處理、但在正式專案中值得補上的部分。

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