DIV CSS 佈局教程網

用 JS 實作一個簡單的鎖屏功能
編輯:JavaScript綜合知識     

「鎖屏」是一種常見的互動效果:當使用者暫時離開時,可以鎖住畫面並保留工作狀態,回來後輸入密碼驗證即可還原。本文示範如何用 JavaScript 實作一個簡單的鎖屏功能,並說明框架(frame)網站下的特殊處理方式。

基本思路

最直觀的鎖屏做法,是在頁面上蓋一層半透明遮罩,再於中央放一個承載密碼驗證表單的 iframe 視窗。以下 LockScreen 函式在呼叫時傳入 true 會建立遮罩與中央視窗,傳入 false 則隱藏它們以解鎖:

完整程式碼

// ********* 鎖屏 DIV ***************************
function LockScreen(tag, title, width, height, url) {
  if (tag) {
    // 鎖屏
    var lockdiv = document.getElementById("lockscreen");
    if (lockdiv != null) {
      lockdiv.style.display = "block";
      var subdiv = document.getElementById("subdialog");
      if (subdiv != null) {
        subdiv.style.display = "block";
        document.getElementById("dialog1").src = url;
      }
    } else {
      // 創建新的鎖屏 DIV,並執行鎖屏
      var tabframe = document.createElement("div");
      tabframe.id = "lockscreen";
      tabframe.name = "lockscreen";
      tabframe.style.top = '0px';
      tabframe.style.left = '0px';
      tabframe.style.height = '100%';
      tabframe.style.width = '100%';
      tabframe.style.position = "absolute";
      tabframe.style.filter = "Alpha(opacity=10)";
      tabframe.style.backgroundColor = "#000000";
      tabframe.style.zIndex = "99998";
      document.body.appendChild(tabframe);
      tabframe.style.display = "block";

      // 子 DIV
      var subdiv = document.createElement("div");
      subdiv.id = "subdialog";
      subdiv.name = "subdialog";
      subdiv.style.top = Math.round((tabframe.clientHeight - height) / 2);
      subdiv.style.left = Math.round((tabframe.clientWidth - width) / 2);
      subdiv.style.height = height + 'px';
      subdiv.style.width = width + 'px';
      subdiv.style.position = "absolute";
      subdiv.style.backgroundColor = "#000000";
      subdiv.style.zIndex = "99999";
      subdiv.style.filter = "Alpha(opacity=100)";
      subdiv.style.border = "1px";
      document.body.appendChild(subdiv);
      subdiv.style.display = "block";

      var iframe_height = height - 30;
      var titlewidth = width;
      var html = "<table border='0' cellpadding='0' cellspacing='0'>";
      html += "<tr><td></td><td>";
      html += "<table><tr><td><font color='#FFFFFF'><b>" + title + "</b></font></td>" +
              "<td style='width:30px' valign='top'><img src='/images/images/close.gif'></img></td></tr></table>";
      html += "</td><td></td></tr>";
      html += "<tr><td></td><td style='height:100px;'>" +
              "<iframe id='dialog1' frameborder=0 style='width:" + titlewidth + "px;height:" +
              iframe_height + "px' src='" + url + "'></iframe></td><td></td></tr>";
      html += "<td></td><td></td><td></td>";
      html += "</table>";
      subdiv.innerHTML = html;
    }
  } else {
    // 解屏
    var lockdiv = document.getElementById("lockscreen");
    if (lockdiv != null) {
      lockdiv.style.display = "none";
    }
    var subdiv = document.getElementById("subdialog");
    if (subdiv != null) {
      subdiv.style.display = "none";
    }
  }
}
function UNLockScreen() {
  LockScreen(false);
}

框架網站的特殊處理

一般的鎖屏是靠遮罩層或兩塊區域互相顯示/隱藏來實現。但若是用 frame 建構的網站,做法就棘手得多——因為框架頁面無法用 div 疊層,也不支援 display:none。

解法是在 FRAMESET 內「再多加一個 frame」:平時把這個 frame 的高度設為 0;點擊鎖屏時,把其他 frame 的高度設為 0,並把這個新 frame 的高度設為 *(佔滿),就完成了 frame 的「替換」。解鎖時再把 FRAMESET 的 rows 屬性還原即可。

防止繞過鎖屏

如果只做到這裡,使用者仍可透過右鍵重新整理或按 F5 來繞過密碼驗證。因此需要攔截 F5 與右鍵的預設行為:

// 阻止 F5 或者滑鼠右鍵重新整理,使鎖屏失效。
document.onkeydown = function () {
  if (event.keyCode == 116) {
    event.keyCode = 0;
    event.returnValue = false;
  }
};
document.oncontextmenu = function () {
  event.returnValue = false;
};

呼叫方式

最後只要呼叫即可啟動鎖屏:

LockScreen(true, '標題', 424, 314, 'https://www.baidu.com');

要補充的是,這種前端鎖屏屬於「視覺層面的攔阻」,主要用於提升使用體驗,並不能取代真正的身分驗證機制;敏感操作仍應由後端把關。希望本文對大家的 JavaScript 開發有所幫助。

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