確認ダイアログ(Confirm Modal Dialog)を自作する方法

今回は確認ダイアログ(confirm)を自作する方法を紹介します。基本的にはモーダル(Modal)ダイアログの一種ですね。ほぼ同じです(笑)。

確認ダイアログ(Confirm Modal Dialog)を自作する方法

サンプル

HTMLコード

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>Confirm Dialog Sample(1)</title>
  <style>
    /* ボタンのスタイル定義 */
    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;
    }
    /* Float キャンセル/ OK ボタンに同じ幅を定義 */
    .cancelbtn, .okebtn {
      float: left;
      width: 50%;
    }
    /* キャンセルボタン定義 */
    .cancelbtn {
      background-color: #ccc;
      color: black;
    }
    /* OKボタン定義 */
    .okebtn {
      background-color:#0d6efd;
    }
    /* 中央配置 */
    .container {
      padding: 16px;
      text-align: center;
    }
    /* モーダル(背景)*/
    .modal {
      transition: 0.6s;
      visibility: hidden;
      opacity: 0;
      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; 
      border: 1px solid #888;
      width: 50%; /*モーダルダイアログの幅*/
    }
     
    /* ×(閉じる)ボタン定義 */
    .close {
      position: absolute;
      right: 35px;
      top: 15px;
      font-size: 40px;
      font-weight: bold;
      color: #f1f1f1;
    }
    .close:hover,
    .close:focus {
      color: #f44336;
      cursor: pointer;
    }
    /* clearfix定義 */
    .clearfix::after {
      content: "";
      clear: both;
      display: table;
    }
    /* 小さいディスプレイの場合はボタンのスタイルを変更します */
    @media screen and (max-width: 300px) {
      .cancelbtn, .okebtn {
         width: 100%;
      }
    }
  </style>
</head>
<body>
  <h2>確認ダイアログ</h2>
  <div style="width:200px">
  <button onclick="openDialog();">モーダルを開く</button>
  </div>
  <div id="id01" class="modal">
    <span onclick="closeDialog();" class="close" title="モーダルを閉じる">&times;</span>
    <form class="modal-content" action="#">
      <div class="container">
        <h1>確認ダイアログ</h1>
        <p>登録してもよろしいでしょうか?</p>
        <div class="clearfix">
          <button type="button" class="cancelbtn" onclick="closeDialog();">キャンセル</button>
          <button type="button" class="okebtn" onclick="document.forms[0].submit();">ОK</button>
        </div>
      </div>
    </form>
  </div>
  <script>
    // モーダル取得
    var modal = document.getElementById('id01');
    // モーダルダイアログ領域外をクリックした場合、ダイアログを閉じます
    window.onclick = function(event) {
      if (event.target == modal) {
        closeDialog();
      }
    }
    // ダイアログを開く処理
    function openDialog() {
      var modal = document.getElementById('id01');
      modal.style.opacity = 1
      modal.style.visibility = "visible";
    }
    // ダイアログを閉じる処理
    function closeDialog() {
      var modal = document.getElementById('id01');
      modal.style.opacity = 0;
       modal.style.visibility = "hidden";
    }
  </script>
</body>
</html>

デモ

補足

完全にモーダルロックにするには?

ダイアログ表示時に、背景をクリックしてもダイアログを閉じないようにするには以下のコードを削除してください。

  <script>
    // モーダル取得
    var modal = document.getElementById('id01');
    // モーダルダイアログ領域外をクリックした場合、ダイアログを閉じます
    window.onclick = function(event) {
      if (event.target == modal) {
        closeDialog();
      }
    }
  ...
  </script>

ご参考

関連ページ