模态框

模态框用于在单击按钮时显示对话框或框。

类名
类型
modal
组件
模态框
modal-box
部件
内容部分
modal-action
部件
操作部分 (按钮等)
modal-backdrop
部件
当模态框打开时覆盖页面的标签,以便我们可以通过单击外部关闭模态框
modal-toggle
部件
控制模态框状态的隐藏复选框
modal-open
修饰符
保持模态框打开 (您可以使用 JS 添加此类)
modal-top
位置
将模态框移动到顶部
modal-middle
位置
将模态框移动到中间[默认]
modal-bottom
位置
将模态框移动到底部
modal-start
位置
将模态框水平移动到起始位置
modal-end
位置
将模态框水平移动到结束位置

有 3 种方法可以使用模态框

  1. 使用 HTML <dialog> 元素
    它需要 JS 才能打开,但它具有更好的可访问性,我们可以使用以下方式关闭它Esc
  2. 使用复选框
    一个隐藏的<input type="checkbox">来控制模态框的状态,以及<label>来选中/取消选中复选框并打开/关闭模态框
  3. 使用 <a> 锚链接
    链接向 URL 添加参数,您只有在 URL 具有该参数时才看到模态框

打开模态框会添加一个scrollbar-gutter到页面,以避免在具有固定滚动条的操作系统上发生布局偏移。
如果您不想使用此功能,您可以排除 rootscrollgutter.

方法 1. HTML dialog 元素推荐

HTML dialog 元素是创建模态框的原生方法。它具有可访问性,我们可以使用以下方式关闭模态框Esc键。
我们可以使用 JS 打开模态框ID.showModal()方法,并使用以下方式关闭它ID.close()方法。
ID 对于每个模态框必须是唯一的。

对话框模态框

单击时使用 ID.showModal() 方法打开。可以使用 ID.close() 方法关闭
<!-- Open the modal using ID.showModal() method -->
<button class="$$btn" onclick="my_modal_1.showModal()">open modal</button>
<dialog id="my_modal_1" class="$$modal">
  <div class="$$modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="$$modal-action">
      <form method="dialog">
        <!-- if there is a button in form, it will close the modal -->
        <button class="$$btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
{/* Open the modal using document.getElementById('ID').showModal() method */}
<button className="$$btn" onClick={()=>document.getElementById('my_modal_1').showModal()}>open modal</button>
<dialog id="my_modal_1" className="$$modal">
  <div className="$$modal-box">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click the button below to close</p>
    <div className="$$modal-action">
      <form method="dialog">
        {/* if there is a button in form, it will close the modal */}
        <button className="$$btn">Close</button>
      </form>
    </div>
  </div>
</dialog>

对话框模态框,单击外部时关闭

有一个带有 'modal-backdrop' 类的第二个表单,它覆盖屏幕,因此我们可以在单击外部时关闭模态框
<!-- Open the modal using ID.showModal() method -->
<button class="$$btn" onclick="my_modal_2.showModal()">open modal</button>
<dialog id="my_modal_2" class="$$modal">
  <div class="$$modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click outside to close</p>
  </div>
  <form method="dialog" class="$$modal-backdrop">
    <button>close</button>
  </form>
</dialog>
{/* Open the modal using document.getElementById('ID').showModal() method */}
<button className="$$btn" onClick={()=>document.getElementById('my_modal_2').showModal()}>open modal</button>
<dialog id="my_modal_2" className="$$modal">
  <div className="$$modal-box">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click outside to close</p>
  </div>
  <form method="dialog" className="$$modal-backdrop">
    <button>close</button>
  </form>
</dialog>

带有角落关闭按钮的对话框模态框

<!-- You can open the modal using ID.showModal() method -->
<button class="$$btn" onclick="my_modal_3.showModal()">open modal</button>
<dialog id="my_modal_3" class="$$modal">
  <div class="$$modal-box">
    <form method="dialog">
      <button class="$$btn $$btn-sm $$btn-circle $$btn-ghost absolute right-2 top-2"></button>
    </form>
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click on ✕ button to close</p>
  </div>
</dialog>
{/* You can open the modal using document.getElementById('ID').showModal() method */}
<button className="$$btn" onClick={()=>document.getElementById('my_modal_3').showModal()}>open modal</button>
<dialog id="my_modal_3" className="$$modal">
  <div className="$$modal-box">
    <form method="dialog">
      {/* if there is a button in form, it will close the modal */}
      <button className="$$btn $$btn-sm $$btn-circle $$btn-ghost absolute right-2 top-2"></button>
    </form>
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click on ✕ button to close</p>
  </div>
</dialog>

具有自定义宽度的对话框模态框

您可以使用任何 w-
<!-- You can open the modal using ID.showModal() method -->
<button class="$$btn" onclick="my_modal_4.showModal()">open modal</button>
<dialog id="my_modal_4" class="$$modal">
  <div class="$$modal-box w-11/12 max-w-5xl">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Click the button below to close</p>
    <div class="$$modal-action">
      <form method="dialog">
        <!-- if there is a button, it will close the modal -->
        <button class="$$btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
{/* You can open the modal using document.getElementById('ID').showModal() method */}
<button className="$$btn" onClick={()=>document.getElementById('my_modal_4').showModal()}>open modal</button>
<dialog id="my_modal_4" className="$$modal">
  <div className="$$modal-box w-11/12 max-w-5xl">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Click the button below to close</p>
    <div className="$$modal-action">
      <form method="dialog">
        {/* if there is a button, it will close the modal */}
        <button className="$$btn">Close</button>
      </form>
    </div>
  </div>
</dialog>

响应式

模态框在 SM 屏幕尺寸上移至底部,在 MD 屏幕尺寸上移至中间
<!-- Open the modal using ID.showModal() method -->
<button class="$$btn" onclick="my_modal_5.showModal()">open modal</button>
<dialog id="my_modal_5" class="$$modal $$modal-bottom sm:$$modal-middle">
  <div class="$$modal-box">
    <h3 class="text-lg font-bold">Hello!</h3>
    <p class="py-4">Press ESC key or click the button below to close</p>
    <div class="$$modal-action">
      <form method="dialog">
        <!-- if there is a button in form, it will close the modal -->
        <button class="$$btn">Close</button>
      </form>
    </div>
  </div>
</dialog>
{/* Open the modal using document.getElementById('ID').showModal() method */}
<button className="$$btn" onClick={()=>document.getElementById('my_modal_5').showModal()}>open modal</button>
<dialog id="my_modal_5" className="$$modal $$modal-bottom sm:$$modal-middle">
  <div className="$$modal-box">
    <h3 className="font-bold text-lg">Hello!</h3>
    <p className="py-4">Press ESC key or click the button below to close</p>
    <div className="$$modal-action">
      <form method="dialog">
        {/* if there is a button in form, it will close the modal */}
        <button className="$$btn">Close</button>
      </form>
    </div>
  </div>
</dialog>

方法 2. 复选框旧版

隐藏的复选框可以控制模态框的状态,标签可以切换复选框,因此我们可以打开/关闭模态框。

链接向 URL 添加参数,您只有在 URL 具有该参数时才看到模态框
当模态框关闭时,页面将滚动到顶部,因为使用了锚链接。 锚链接可能在某些 SPA 框架上无法正常工作。如果出现问题,请使用其他方法

daisyUI store

NEXUS
官方 daisyUI 仪表板模板

在 daisyUI 商店有售

更多详情