弹窗从鼠标点击位置弹起
![](https://img2022.cnblogs.com/blog/2273723/202205/2273723-20220510234028893-2119611876.gif)
代码
<!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>
body {
margin: 0;
background-color: rgba(79, 185, 255, 0.7);
}
.mask {
width: 100vw;
height: 100vh;
position: fixed;
transition: opacity 0.3s, transform 0.3s, visibility 0.3s;
background-color: rgba(92, 92, 92, 0.7);
}
.dialog {
transition: transform 0.3s, width 0.3s, height 0.3s;
background-color: rgb(255, 255, 255);
}
.fill-screen {
width: 100vw;
height: 100vh;
overflow: auto;
}
.half-screen {
width: 50vw;
height: 80vh;
overflow: auto;
}
.grid-center {
display: grid;
place-items: center;
}
.scale-out {
transform: scale(0);
}
.scale-in {
transform: scale(1);
}
.hidden {
visibility: hidden;
opacity: 0;
}
.visible {
visibility: visible;
opacity: 1;
}
</style>
</head>
<body class="fill-screen">
<div class="mask grid-center hidden">
<div class="dialog fill-screen scale-out"></div>
</div>
<script>
const mask = document.querySelector('.mask');
const dialog = document.querySelector('.dialog');
function enter(e) {
dialog.style.transformOrigin = `${e.x}px ${e.y}px`;
mask.classList.add('visible');
dialog.classList.add('scale-in');
dialog.classList.add('half-screen');
}
function leave() {
mask.classList.remove('visible');
dialog.classList.remove('scale-in');
dialog.classList.remove('half-screen');
}
document.addEventListener('click', enter);
document.addEventListener('keydown', e => e.key === 'Escape' && leave());
mask.addEventListener('click', e => {
e.stopPropagation();
e.target === mask && leave();
});
</script>
</body>
</html>