js實現簡單鎖屏功能執行個體_javascript技巧

來源:互聯網
上載者:User

本文執行個體講述了js實現簡單鎖屏功能的方法。分享給大家供大家參考。具體實現方法如下:

//********* 鎖屏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";      //subdiv.onmousemove = mouseMoveDialog;      //subdiv.onmousedown = control_onmousedown;      //subdiv.onmouseup = mouseUp;      document.body.appendChild(subdiv);      subdiv.style.display = "block";      //subdiv.onclick=UNLockScreen;      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);}

如果大家不知道什麼是鎖屏,可以去163信箱看一看,用途是你要離開螢幕一段時間時可以暫時鎖住螢幕保留工作空間。帶回來只要重新輸入密碼驗證即可恢複到原先的工作空間。

一般都是通過在頁面上增加不透明遮罩層實現鎖屏功能,或者是使用兩個地區互相顯示隱藏。使用架構(frame)構建的網站如果要實現鎖屏功能則很有難度。因為在架構頁面無法使用div做層。而且架構也不支援css的display:none;屬性。

最後的實現方法是使用在FRAMESET內再增加一個frame,出事狀態時FRAMESET的rows屬性將新增加的frame設定為高度為0。點擊鎖屏按鈕時,則將FRAMESET中其他的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,'http://www.baidu.com');

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.