<!DOCTYPE html>
<html>
<head>
<title>测试</title>
<style>
html {
font-size: 14px;
}
html,
body {
margin: 0;
padding: 0;
height: 100vh;
width: 100vw;
}
.dialog {
top: 0;
left: 0;
position: fixed;
display: none;
width: 100vw;
height: 100vh;
z-index: 999;
}
.mask {
background-color: rgba(0, 0, 0, 0.3);
height: 100%;
width: 100%;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
.d_container {
background-color: #fff;
width: 60%;
height: 50%;
position: absolute;
}
.d_header {
background-color: #f2f4f7;
height: 3.5rem;
display: flex;
}
.drag {
height: 100%;
width: calc(100% - 3.5rem);
display: flex;
align-items: center;
padding: 0 2rem;
box-sizing: border-box;
cursor: move;
user-select: none;
}
.close {
height: 100%;
width: 3.5rem;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
}
.close:hover {
background-color: red;
color: #fff;
}
.d_content {
height: calc(100% - 3.5rem);
width: 100%;
}
</style>
<script>
window.onload = function () {
drag()
}
function drag() {
let list = document.querySelector('.drag')
let box1 = document.querySelector('.drag_box')
list.onmousedown = function (e) {//点击的位置 移动的距离 移动距离
let boxleft = box1.getBoundingClientRect().left
let boxtop = box1.getBoundingClientRect().top
let x = e.x
let y = e.y
document.onmousemove = function (d) {
box1.style.left = boxleft + d.x - x + 'px'
box1.style.top = boxtop + d.y - y + 'px'
}
document.onmouseup = function () {
document.onmousemove = null;
}
}
}
/* 关闭弹窗 */
function closeDialog(e) {
e.stopPropagation();
let dialog = document.querySelector('.dialog')
dialog.style.display = "none"
}
/* 开启弹窗 */
function openDialog() {
let dialog = document.querySelector('.dialog')
dialog.style.display = "block"
}
</script>
</head>
<body>
<div class="container">
<button onclick="openDialog()">打开弹窗</button>
<div class="dialog">
<div class="mask">
<div class="d_container drag_box">
<div class="d_header">
<div class="drag">
<div class="d_h_title">
详情
</div>
</div>
<div onclick="closeDialog(event)" class="close">X</div>
</div>
<div class="d_content">
<table>
<thead></thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
js 弹窗拖动
于 2022-05-26 13:00:58 首次发布