<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.login_bg {
display: none;
position: absolute;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.6);
z-index: 1;
}
h1 {
text-align: center;
}
.login_box {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
text-align: center;
width: 400px;
height: 200px;
background-color: pink;
z-index: 2;
}
</style>
</head>
<body>
<div id='bg' class="login_bg"></div>
<h1>点击弹出登录框</h1>
<div class="login_box">
<h3>会员登录</h3>
<div> 用户名:<input type="text" value="请输入用户名"></div>
<div> 密码:<input type="password" value="请输入登录密码"></div>
<button class="link">登录会员</button>
<button class="close">关闭</button>
<script>
var login_box = document.querySelector('.login_box');
var mask = document.querySelector('.login_bg');
var h1 = document.querySelector('h1');
var close = document.querySelector('.close');
var link = document.querySelector('.link');
var h3 = document.querySelector('h3');
h1.addEventListener('click', function() {
login_box.style.display = 'block';
mask.style.display = 'block';
});
close.addEventListener('click', function() {
login_box.style.display = 'none';
mask.style.display = 'none';
});
// 获取鼠标在盒子内的鼠标坐标
h3.addEventListener('mousedown', function(e) {
var x = e.pageX - login_box.offsetLeft;
var y = e.pageY - login_box.offsetTop;
// 边框到文档边缘的坐标
document.addEventListener('mousemove', fn);
function fn(e) {
login_box.style.left = e.pageX - x + 'px';
login_box.style.top = e.pageY - y + 'px';
}
// 鼠标谈起移动事件移除
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', fn);
});
});
</script>
</div>
</body>
</html>
拖动模态框
最新推荐文章于 2022-09-05 20:23:46 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)