<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.login-header {
width: 100%;
text-align: center;
height: 30px;
font-size: 24px;
line-height: 30px;
}
ul,
li,
ol,
dl,
dt,
dd,
div,
p,
span,
h1,
h2,
h3,
h4,
h5,
h6,
a {
padding: 0px;
margin: 0px;
}
.login {
width: 512px;
height: 280px;
position: absolute;
left: 0;
right: 0;
border: #ebebeb solid 1px;
background: #ffffff;
box-shadow: 0px 0px 20px #ddd;
z-index: 9999;
display: none;
}
.login-title {
width: 100%;
margin: 10px 0px 0px 0px;
text-align: center;
line-height: 40px;
height: 40px;
font-size: 18px;
position: relative;
cursor: move; // 鼠标可移动光标
user-select: none; // 去除浏览器默认样式的。
/* 火狐 */
/* -moz-user-select: none; */
/*webkit浏览器*/
/* -webkit-user-select: none; */
/*IE10*/
/* -ms-user-select: none; */
/*早期浏览器*/
/* -khtml-user-select: none; */
/* user-select: none; */
}
.login-input-content {
margin-top: 20px;
}
.login-button {
width: 50%;
margin: 30px auto 0px auto;
line-height: 40px;
font-size: 14px;
border: #ebebeb 1px solid;
text-align: center;
}
.login-bg {
width: 100%;
height: 100%;
position: fixed;
top: 0px;
left: 0px;
background: #000000;
filter: alpha(opacity=30);
opacity: 0.3;
display: none;
}
a {
text-decoration: none;
color: #000000;
}
.login-button a {
display: block;
}
.login-input input.list-input {
float: left;
line-height: 35px;
height: 35px;
width: 350px;
border: #ebebeb 1px solid;
text-indent: 5px;
}
.login-input {
overflow: hidden;
margin: 0px 0px 20px 0px;
}
.login-input label {
float: left;
width: 90px;
padding-right: 10px;
text-align: right;
line-height: 35px;
height: 35px;
font-size: 14px;
}
.login-title span {
position: absolute;
font-size: 12px;
right: -20px;
top: -30px;
background: #ffffff;
border: #ebebeb solid 1px;
width: 40px;
height: 40px;
border-radius: 20px;
z-index: 99;
cursor: pointer; // 鼠标手型光标
}
</style>
</head>
<body>
<div class="login-header"><a id="link" href="javascript:void(0);">点击,弹出登录框</a></div>
<div id="login" class="login">
<div id="title" class="login-title">登录会员
<span id="closeBtn">关闭</span></div>
<div class="login-input-content">
<div class="login-input">
<label>用户名:</label>
<input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
</div>
<div class="login-input">
<label>登录密码:</label>
<input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input">
</div>
</div>
<div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div>
</div>
<script>
// 实现思路:
// 1. 找对象
// 2. 给link注册click
// 3. login 显示出来,并且需要水平和垂直居中效果
// div的left: (可视区的宽度 - div的宽度 ) / 2
// 4. 在 title上鼠标摁下去,可以拖拽login进行移动
// 阀门一开始是关闭的, 阀门是用来控制onmousemove 事件
// 4.1 给title注册 onmousedown, 阀门才打开
// 4.2 给document 注册 onmosuemove, 让login 跟随鼠标进行移动
// 把鼠标移动的时候鼠标的位置 - 鼠标在login上按下去的距离
// 鼠标在login上按下去的距离 ==> 鼠标在页面中的位置 - login到左边的距离
// 4.3 给 document 注册 omouseup ,让login不在跟随鼠标移动,, 阀门给关闭了
//
// 5. 限制login的拖拽范围
// 在拖拽的时候,left top的值
// 6. 给关闭按钮注册click, 点击关闭login
// 1.
var link = document.querySelector("#link");
var title = document.querySelector("#title");
var login = document.querySelector("#login");
var closeBtn = document.querySelector("#closeBtn");
// 2.
link.onclick = function () {
// 3.
login.style.display = "block";
login.style.left = (window.innerWidth - login.offsetWidth) / 2 + "px";
login.style.top = (window.innerHeight - login.offsetHeight) / 2 + "px";
}
// 4.
var open = false;
var disX = 0;
var disY = 0;
title.onmousedown = function (e) {
open = true;
// 这里面需要注意,不能通过 title去获取登录框到页面左边的距离,这样值为0.因为和结构样式相关
// disX = e.pageX - title.offsetLeft
disX = e.pageX - login.offsetLeft;
disY = e.pageY - login.offsetTop;
}
document.onmousemove = function (e) {
if (open) {
var currentX = e.pageX - disX;
var currentY = e.pageY - disY;
// 限制login的范围,其实就是限制两个变量值的范围
/*if(currentX < 0){
// 左边
currentX = 0;
}*/
/*if(currentY < 0){
// 上边
currentY = 0;
}*/
/*if(currentY > (window.innerHeight - login.offsetHeight)){
// 下边
currentY = window.innerHeight - login.offsetHeight;
}*/
/*if(currentX > (window.innerWidth - login.offsetWidth - 21)){
// 右边
currentX = window.innerWidth - login.offsetWidth - 21;
}*/
window.innerHeight 浏览器内容的高度
login.offsetHeight login自身距离有定位父级元素的高度
currentX = Math.max(currentX, 0);
currentY = Math.max(currentY, 0);
currentY = Math.min(currentY, window.innerHeight - login.offsetHeight);
currentX = Math.min(currentX, window.innerWidth - login.offsetWidth - 21);
login.style.left = currentX + "px";
login.style.top = currentY + "px";
// 字体禁止选中效果
// window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
}
}
document.onmouseup = function () {
open = false;
}
// 关闭按钮
closeBtn.onclick = function () {
login.style.display = "none";
}
**// 节流阀: 让onmousemove 事件有限制的去执行**
// open = false; // 阀门是关闭的状态
// 鼠标摁下去 open = true;
// 鼠标抬起来 open = false;
// 鼠标移动的时候,判断阀门的开启和关闭状态,只有在开启的时候才能触发事件
</script>
</body>
</html>
节流阀之拖拽登录框
最新推荐文章于 2022-09-29 15:37:48 发布