モダルを削除する方法
CSSを使って削除確認モダルを作成する方法を学びます。
ボタンをクリックしてモダルを開く:
×
削除モダルを作成する方法
第1ステップ - HTMLを追加:
モダルを開く <div id="id01" class="modal"> × <form class="modal-content" action="/action_page.php"> <div class="container"> <h1>アカウント削除</h1> <p>本当にアカウントを削除したいですか?</p> <div class="clearfix"> <button type="button" class="cancelbtn">Cancel</button> <button type="button" class="deletebtn">Delete</button> </div> </div> </form> </div>
第2步 - CSSを追加:
* {box-sizing: border-box} /* 全てのボタンにスタイルを設定 */ button { background-color: #04AA6D; color: white; padding: 14px 20px; margin: 8px 0; border: none; cursor: pointer; width: 100%; opacity: 0.9; } button:hover { opacity:1; } /* キャンセルと削除ボタンをフロートし、等しい幅を追加 */ .cancelbtn, .deletebtn { float: left; width: 50%; } /* キャンセルボタンに色を追加 */ .cancelbtn { background-color: #ccc; color: black; } /* 削除ボタンに色を追加 */ .deletebtn { background-color: #f44336; } /* コンテナに内余白を追加し、テキストを中央に配置 */ .container { padding: 16px; text-align: center; } /* モーダル(背景) */ .modal { display: none; /* 默认隐藏 */ position: fixed; /* 固定在原地 */ z-index: 1; /* 置于顶层 */ left: 0; top: 0; width: 100%; /* 全宽 */ height: 100%; /* 全高 */ overflow: auto; /* スクロールを有効にする(必要に応じて) */ background-color: #474e5d; padding-top: 50px; } /* モダルの内容/ボックス */ .modal-content { background-color: #fefefe; margin: 5% auto 15% auto; /* 顶部 5%、底部 15%、中央に 5% */ border: 1px solid #888; width: 80%; /* 幅はスクリーンサイズに応じて変更可能 */ } /* 水平線のスタイルを設定 */ hr { border: 1px solid #f1f1f1; margin-bottom: 25px; } /* モダルの閉じるボタン(x) */ .close { position: absolute; right: 35px; top: 15px; font-size: 40px; font-weight: bold; color: #f1f1f1; } .close:hover { .close:focus { color: #f44336; cursor: pointer; } /* フローツリーをクリア */ .clearfix::after { content: ""; clear: both; display: table; } /* 超小画面上のキャンセルボタンと削除ボタンのスタイルを変更 */ @media screen and (max-width: 300px) { .cancelbtn, .deletebtn { width: 100%; } }
ヒント:以下のJavaScriptコードを使用して、モダルの内容の外側をクリックしてモダルを閉じることができます(「x」や「キャンセル」ボタンをクリックして閉じる以外ではありません):
インスタンス
<script> // 获取模态框 var modal = document.getElementById('id01'); // 当用户点击模态框之外的任何位置时,将其关闭 window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script>