模拟 Element ui 的 Dialog

 最近发现了一个新标签,dialog 正好尝试一下模拟 Element 的 Dialog,代码见下

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0"
    />
    <title>Dialog</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }

      #dialog {
        margin: auto;
        padding: 20px;
        background-color: aquamarine;
        transition: opacity 0.5s ease, transform 1s ease;
      }

      #dialog .title {
        display: block;
      }

      #dialog::backdrop {
        background-color: aqua;
        transition: background-color 0.5s ease;
      }

      #dialog[data-close] {
        opacity: 0;
        transform: perspective(1000px) rotateX(45deg) translateY(-100px);
        /* transform: translate3d(0,-100px,1000px); */
      }

      #dialog[data-close]::backdrop {
        background-color: transparent;
      }
    </style>
  </head>
  <body>
    <div>
      <button onclick="openDialog()">Open</button>
    </div>
    <div>
      <dialog id="dialog">
        <title class="title">Title</title>
        <input
          type="text"
          value="Test"
        />
        <input
          type="button"
          value="Submit"
          onclick="submit()"
        />
      </dialog>
    </div>
  </body>

  <script>
    const dialog = document.getElementById("dialog");

    // const openDialog = () => {
    //   dialog.showModal();
    // };
    // const submit = () => {
    //   dialog.close();
    // };

    const init = () => {
      dialog.showModal();
      dialog.addEventListener("transitionend", (e) => {
        const name = e.propertyName;
        if (dialog.hasAttribute("data-close") && name === "transform") {
          dialog.close();
        }
      });
    };

    const submit = () => {
      dialog.setAttribute("data-close", true);
    };

    const openDialog = () => {
      dialog.showModal();
      dialog.removeAttribute("data-close");
    };

    init();
  </script>
</html>

本来还想看看能不能直接全部 Css 处理,我还是太菜了,只能借助于 Js 实现了部分效果

这个 dialog 标签还是很强的

<dialog>:对话框元素 - HTML(超文本标记语言) | MDN

所以特殊场景可以用自己写的 Dialog 来实现某些效果,今天就这样。

祝大家春节开心,天天快乐,拜拜!

  • 11
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值