弹窗从鼠标点击位置弹起

弹窗从鼠标点击位置弹起

代码

<!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>
posted @ 2022-05-10 23:42  梦渊同学  阅读(149)  评论(0编辑  收藏  举报