javascript实现锁定网页、密码解锁效果(类似系统屏幕保护效果)

功能描述:打开一个网站的网页,过5分钟不动作,就会锁定页面,隐藏内容容器,显示一个容器用于输入密码,输入正确的密码来解锁。锁定后即使用户刷新页面,还是保留原来的状态。如已经锁定的,需要继续锁定,否则显示内容。
 
示例代码如下,通过document.onmouseover来实现多少分钟没有动作,使用计时器来实现。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns = "http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
< title >javascript实现系统屏幕保护效果(锁定网页)</ title >
</ head >
 
< 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 >
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值