模态框用于在单击按钮时显示对话框或框。
类名 | 类型 | |
---|---|---|
modal | 组件 | 模态框 |
modal-box | 部件 | 内容部分 |
modal-action | 部件 | 操作部分 (按钮等) |
modal-backdrop | 部件 | 当模态框打开时覆盖页面的标签,以便我们可以通过单击外部关闭模态框 |
modal-toggle | 部件 | 控制模态框状态的隐藏复选框 |
modal-open | 修饰符 | 保持模态框打开 (您可以使用 JS 添加此类) |
modal-top | 位置 | 将模态框移动到顶部 |
modal-middle | 位置 | 将模态框移动到中间[默认] |
modal-bottom | 位置 | 将模态框移动到底部 |
modal-start | 位置 | 将模态框水平移动到起始位置 |
modal-end | 位置 | 将模态框水平移动到结束位置 |
<dialog>
元素Esc
键<input type="checkbox">
来控制模态框的状态,以及<label>
来选中/取消选中复选框并打开/关闭模态框<a>
锚链接打开模态框会添加一个scrollbar-gutter到页面,以避免在具有固定滚动条的操作系统上发生布局偏移。
如果您不想使用此功能,您可以排除rootscrollgutter
.
推荐
HTML dialog 元素是创建模态框的原生方法。它具有可访问性,我们可以使用以下方式关闭模态框Esc
键。
我们可以使用 JS 打开模态框ID.showModal()
方法,并使用以下方式关闭它ID.close()
方法。
ID 对于每个模态框必须是唯一的。
<!-- 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 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>
{/* 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>
<!-- 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 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>
{/* 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 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>
{/* 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>
<!-- 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 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>
{/* 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>
<!-- 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 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>
{/* 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>
旧版
隐藏的复选框可以控制模态框的状态,标签可以切换复选框,因此我们可以打开/关闭模态框。
<!-- The button to open modal --> <label for="my_modal_6" class="$$btn">open modal</label> <!-- Put this part before </body> tag --> <input type="checkbox" id="my_modal_6" class="$$modal-toggle" /> <div class="$$modal" role="dialog"> <div class="$$modal-box"> <h3 class="text-lg font-bold">Hello!</h3> <p class="py-4">This modal works with a hidden checkbox!</p> <div class="$$modal-action"> <label for="my_modal_6" class="$$btn">Close!</label> </div> </div> </div>
<!-- The button to open modal -->
<label for="my_modal_6" class="$$btn">open modal</label>
<!-- Put this part before </body> tag -->
<input type="checkbox" id="my_modal_6" class="$$modal-toggle" />
<div class="$$modal" role="dialog">
<div class="$$modal-box">
<h3 class="text-lg font-bold">Hello!</h3>
<p class="py-4">This modal works with a hidden checkbox!</p>
<div class="$$modal-action">
<label for="my_modal_6" class="$$btn">Close!</label>
</div>
</div>
</div>
<!-- The button to open modal --> <label for="my_modal_6" class="$$btn">open modal</label> <!-- Put this part before </body> tag --> <input type="checkbox" id="my_modal_6" class="$$modal-toggle" /> <div class="$$modal" role="dialog"> <div class="$$modal-box"> <h3 class="text-lg font-bold">Hello!</h3> <p class="py-4">This modal works with a hidden checkbox!</p> <div class="$$modal-action"> <label for="my_modal_6" class="$$btn">Close!</label> </div> </div> </div>
<!-- The button to open modal -->
<label for="my_modal_6" class="$$btn">open modal</label>
<!-- Put this part before </body> tag -->
<input type="checkbox" id="my_modal_6" class="$$modal-toggle" />
<div class="$$modal" role="dialog">
<div class="$$modal-box">
<h3 class="text-lg font-bold">Hello!</h3>
<p class="py-4">This modal works with a hidden checkbox!</p>
<div class="$$modal-action">
<label for="my_modal_6" class="$$btn">Close!</label>
</div>
</div>
</div>
<!-- The button to open modal --> <label for="my_modal_7" class="$$btn">open modal</label> <!-- Put this part before </body> tag --> <input type="checkbox" id="my_modal_7" class="$$modal-toggle" /> <div class="$$modal" role="dialog"> <div class="$$modal-box"> <h3 class="text-lg font-bold">Hello!</h3> <p class="py-4">This modal works with a hidden checkbox!</p> </div> <label class="$$modal-backdrop" for="my_modal_7">Close</label> </div>
<!-- The button to open modal -->
<label for="my_modal_7" class="$$btn">open modal</label>
<!-- Put this part before </body> tag -->
<input type="checkbox" id="my_modal_7" class="$$modal-toggle" />
<div class="$$modal" role="dialog">
<div class="$$modal-box">
<h3 class="text-lg font-bold">Hello!</h3>
<p class="py-4">This modal works with a hidden checkbox!</p>
</div>
<label class="$$modal-backdrop" for="my_modal_7">Close</label>
</div>
<!-- The button to open modal --> <label for="my_modal_7" class="$$btn">open modal</label> <!-- Put this part before </body> tag --> <input type="checkbox" id="my_modal_7" class="$$modal-toggle" /> <div class="$$modal" role="dialog"> <div class="$$modal-box"> <h3 class="text-lg font-bold">Hello!</h3> <p class="py-4">This modal works with a hidden checkbox!</p> </div> <label class="$$modal-backdrop" for="my_modal_7">Close</label> </div>
<!-- The button to open modal -->
<label for="my_modal_7" class="$$btn">open modal</label>
<!-- Put this part before </body> tag -->
<input type="checkbox" id="my_modal_7" class="$$modal-toggle" />
<div class="$$modal" role="dialog">
<div class="$$modal-box">
<h3 class="text-lg font-bold">Hello!</h3>
<p class="py-4">This modal works with a hidden checkbox!</p>
</div>
<label class="$$modal-backdrop" for="my_modal_7">Close</label>
</div>
旧版
链接向 URL 添加参数,您只有在 URL 具有该参数时才看到模态框
当模态框关闭时,页面将滚动到顶部,因为使用了锚链接。 锚链接可能在某些 SPA 框架上无法正常工作。如果出现问题,请使用其他方法
<!-- The button to open modal --> <a href="#my_modal_8" class="$$btn">open modal</a> <!-- Put this part before </body> tag --> <div class="$$modal" role="dialog" id="my_modal_8"> <div class="$$modal-box"> <h3 class="text-lg font-bold">Hello!</h3> <p class="py-4">This modal works with anchor links</p> <div class="$$modal-action"> <a href="#" class="$$btn">Yay!</a> </div> </div> </div>
<!-- The button to open modal -->
<a href="#my_modal_8" class="$$btn">open modal</a>
<!-- Put this part before </body> tag -->
<div class="$$modal" role="dialog" id="my_modal_8">
<div class="$$modal-box">
<h3 class="text-lg font-bold">Hello!</h3>
<p class="py-4">This modal works with anchor links</p>
<div class="$$modal-action">
<a href="#" class="$$btn">Yay!</a>
</div>
</div>
</div>
<!-- The button to open modal --> <a href="#my_modal_8" class="$$btn">open modal</a> <!-- Put this part before </body> tag --> <div class="$$modal" role="dialog" id="my_modal_8"> <div class="$$modal-box"> <h3 class="text-lg font-bold">Hello!</h3> <p class="py-4">This modal works with anchor links</p> <div class="$$modal-action"> <a href="#" class="$$btn">Yay!</a> </div> </div> </div>
<!-- The button to open modal -->
<a href="#my_modal_8" class="$$btn">open modal</a>
<!-- Put this part before </body> tag -->
<div class="$$modal" role="dialog" id="my_modal_8">
<div class="$$modal-box">
<h3 class="text-lg font-bold">Hello!</h3>
<p class="py-4">This modal works with anchor links</p>
<div class="$$modal-action">
<a href="#" class="$$btn">Yay!</a>
</div>
</div>
</div>
此模态框使用隐藏的复选框!
您已被选中,有机会获得一年免费使用维基百科的订阅!