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

功能描述:打开一个网站的网页,过指定时间不动作,就会锁定页面,隐藏内容容器,显示一个容器用于输入密码,输入正确的密码来解锁。锁定后即使用户刷新页面,还是保留原来的状态。如已经锁定的,需要继续锁定,否则显示内容。
 示例代码如下,通过document.onmouseover来实现多少分钟没有动作,使用计时器来实现。
(此处建议,将下列方法单独提取到一个公共的js中,然后再引用到页面上)

<!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="确定" οnclick="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>

发布了27 篇原创文章 · 获赞 0 · 访问量 831

猜你喜欢

转载自blog.csdn.net/u013232219/article/details/104369664