打開一個網站的網頁,過5分鐘不動作,就會鎖定頁面,隱藏內容容器,顯示一個容器用于輸入密碼,輸入正確的密碼來解鎖。鎖定后即使用戶刷新頁面,還是保留原來的狀態。如已經鎖定的,需要繼續鎖定,否則顯示內容。javascript實現鎖定網頁、密碼解鎖效果,跟Windows系統的屏幕保護效果類似,怎么實現呢?
<body>
<div id="dvContent">內容<br />內容<br />內容<br />內容<br />內容<br />內容</div>
<div id="dvPassword" style="display:none">輸入密碼:<input type="password" id="txtPwd" /><input type="button" value="確定" onclick="check()"/></div>
<script>
if (document.cookie.indexOf('lock=1') != -1) ShowContent(false);
var delay = 10 * 1000,timer;//10s后鎖定,修改delay為你需要的時間,單位毫秒
function startTimer() {
clearTimeout(timer);
timer = setTimeout(TimerHandler, delay);
}
function TimerHandler() {
document.cookie = 'lock=1';
document.onmousemove = null;//鎖定后移除鼠標移動事件
ShowContent(false);
}
function ShowContent(show) {
document.getElementById('dvContent').style.display = show ? 'block' : 'none';
document.getElementById('dvPassword').style.display = show ? 'none' : 'block';
}
function check() {
if (document.getElementById('txtPwd').value == '123') {
document.cookie = 'lock=0';
ShowContent(true);
startTimer()//重新計時
document.onmousemove = startTimer; //重新綁定鼠標移動事件
}
else alert('密碼不正確!!');
}
window.onload = function () {
document.onmousemove = startTimer;
startTimer();
}
</script>
</body>
</html>
|
新聞熱點
疑難解答