window.addEventListener("load", function () { // alert('登陆页面可以关闭哦!') var body = document.querySelector("body"); var box = document.querySelector(".box"); var close = document.querySelector(".close"); var closer = document.querySelector(".close").firstElementChild; // close 范围太广,只取其第一个子元素(即装 X 文本的 span元素) var header = document.querySelector(".header"); var not = document.querySelector(".not"); // 提示文字最好不要改大盒子里直接修改,而是安排一个小盒子 var sub = document.querySelector(".sub"); var time = 5; header.addEventListener("click", function () { body.style.backgroundColor = "rgba(0, 0, 0, 0.5)"; box.style.display = "block"; not.innerHTML = "(拖动顶部可以移动窗口哦 " + time + "! )"; time--; var time2 = setInterval(notice, 1000); function notice() { not.innerHTML = "(拖动顶部可以移动窗口哦 " + time + "! )"; time--; } setTimeout(function () { clearInterval(time2); not.innerHTML = ""; time = 5; // 重新赋值,下次再打开时还是从 5 开始计时 }, 6000); }); closer.onclick = function () { box.style.display = "none"; body.style.backgroundColor = "#fff"; }; document.addEventListener("selectstart", function (e) { e.preventDefault(); //方法后的两个括号不可以省略 // 禁止选中文字,避免给拖动操作带来不好的效果 }); // 开始拖拽 // (1) 鼠标按下, 就获取鼠标在盒子内的坐标 close.addEventListener("mousedown", function (e) { var x = e.pageX - box.offsetLeft; var y = e.pageY - box.offsetTop; // 记录鼠标在 移动窗口(盒子)内的坐标 x y // console.log(x, y); // (2) 当鼠标移动时,设置窗口的 left 和 top 值 ,让其等于鼠标在document的坐标减去在窗口(盒子)内的坐标 document.addEventListener("mousemove", move); function move(e) { box.style.left = e.pageX - x + "px"; box.style.top = e.pageY - y + "px"; } // 鼠标弹起, 就让鼠标移动事件移除 document.addEventListener("mouseup", function () { document.removeEventListener("mousemove", move); }); }); var time3 = 5; sub.addEventListener("click", function () { // alert('haha') sub.value = "您将会在" + time3 + "秒种之后跳转到首页"; time3--; setInterval(function () { if (time3 == 0) { alert("登陆成功,点击确定即可进入页面"); window.location.href = "https://www.youdu.love/"; } sub.value = "您将会在" + time3 + "秒种之后跳转到首页"; time3--; }, 1000); }); });