カスタム確認ダイアログをドロワーメニューで表示する方法【HTML/CSS/JavaScript・サンプルコード付き】
html/css/js
カスタム確認ダイアログをドロワーメニューで表示する方法
HTML・CSS・JavaScriptを使って、カスタム確認ダイアログをドロワーメニュー内に表示する方法を紹介します。このサンプルでは、ボタンをクリックすると画面横から確認用のドロワーメニューが表示され、キャンセルや実行などの操作を選べます。削除確認、ログアウト確認、保存確認、購入確認など、重要な操作前に確認画面を表示したい場合に利用できます。
コードについて
本記事のコードはサンプルコードです。ご利用前に必ず動作確認を行ってください。
免責事項
本コードの利用により発生した損害について、当サイトは一切の責任を負いません。
コードをコピーして使おう!
<!DOCTYPE html>
<html lang="ja">
<head>
<!-- 文字コードを指定 -->
<meta charset="UTF-8">
<!-- スマホ表示に対応 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ページタイトル -->
<title>カスタム確認ダイアログをドロワーメニューで表示</title>
<!-- CSSファイルを読み込み -->
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- デモ全体を囲むエリア -->
<div class="custom-confirm-drawer-menu_wrap">
<!-- 見出し -->
<div class="custom-confirm-drawer-menu_title">
カスタム確認ダイアログをドロワーメニューで表示
</div>
<!-- 説明文 -->
<p class="custom-confirm-drawer-menu_text">
ボタンをクリックすると、確認用のドロワーメニューを表示できます。
</p>
<!-- 確認ダイアログを開くボタン -->
<button class="custom-confirm-drawer-menu_open" type="button" data-drawer-open>
削除する
</button>
<!-- 実行結果メッセージ -->
<div class="custom-confirm-drawer-menu_message" data-result-message></div>
</div>
<!-- ドロワーメニュー背景 -->
<div class="custom-confirm-drawer-menu_overlay" data-drawer-overlay>
<!-- 確認ダイアログ本体 -->
<div class="custom-confirm-drawer-menu_drawer" role="dialog" aria-modal="true">
<!-- 確認ダイアログの見出し -->
<div class="custom-confirm-drawer-menu_drawer-title">
本当に削除しますか?
</div>
<!-- 確認ダイアログの説明文 -->
<p class="custom-confirm-drawer-menu_drawer-text">
この操作はサンプルです。実際の削除処理は行われません。
</p>
<!-- 操作ボタン -->
<div class="custom-confirm-drawer-menu_actions">
<button class="custom-confirm-drawer-menu_cancel" type="button" data-drawer-close>
キャンセル
</button>
<button class="custom-confirm-drawer-menu_submit" type="button" data-drawer-confirm>
実行する
</button>
</div>
</div>
</div>
<!-- JavaScriptファイルを読み込み -->
<script src="script.js"></script>
</body>
</html>
/* ページ全体の設定 */
body{
margin:0;
font-family:sans-serif;
background:#ffffff;
}
/* デモ全体を囲むエリア */
.custom-confirm-drawer-menu_wrap{
max-width:700px;
margin:40px auto;
padding:32px;
border:1px solid #e5e7eb;
border-radius:18px;
background:#f8fafc;
text-align:center;
}
/* タイトル */
.custom-confirm-drawer-menu_title{
font-size:22px;
font-weight:700;
margin-bottom:12px;
}
/* 説明文 */
.custom-confirm-drawer-menu_text{
margin-bottom:24px;
color:#4b5563;
}
/* 確認ダイアログを開くボタン */
.custom-confirm-drawer-menu_open{
padding:12px 24px;
border:none;
border-radius:8px;
background:#dc2626;
color:#ffffff;
font-weight:700;
cursor:pointer;
transition:opacity .2s;
}
/* 開くボタンのホバー */
.custom-confirm-drawer-menu_open:hover{
opacity:.8;
}
/* 実行結果メッセージ */
.custom-confirm-drawer-menu_message{
margin-top:20px;
font-weight:700;
color:#111827;
}
/* ドロワーメニュー背景 */
.custom-confirm-drawer-menu_overlay{
display:none;
position:fixed;
inset:0;
z-index:9999;
background:rgba(0,0,0,.5);
align-items:stretch;
justify-content:flex-start;
}
/* ドロワーメニュー表示時 */
.custom-confirm-drawer-menu_overlay.is-active{
display:flex;
}
/* 確認ダイアログ本体 */
.custom-confirm-drawer-menu_drawer{
width:360px;
max-width:90%;
height:100%;
padding:28px;
background:#ffffff;
box-sizing:border-box;
box-shadow:10px 0 40px rgba(0,0,0,.2);
animation:custom-confirm-drawer-menu_slide .35s ease forwards;
}
/* 確認ダイアログの見出し */
.custom-confirm-drawer-menu_drawer-title{
font-size:22px;
font-weight:700;
margin-bottom:16px;
}
/* 確認ダイアログの説明文 */
.custom-confirm-drawer-menu_drawer-text{
margin-bottom:24px;
color:#4b5563;
line-height:1.8;
}
/* 操作ボタン全体 */
.custom-confirm-drawer-menu_actions{
display:flex;
gap:12px;
}
/* キャンセルボタンと実行ボタン */
.custom-confirm-drawer-menu_cancel,
.custom-confirm-drawer-menu_submit{
flex:1;
padding:12px 16px;
border:none;
border-radius:8px;
color:#ffffff;
font-weight:700;
cursor:pointer;
transition:opacity .2s;
}
/* キャンセルボタン */
.custom-confirm-drawer-menu_cancel{
background:#111827;
}
/* 実行ボタン */
.custom-confirm-drawer-menu_submit{
background:#dc2626;
}
/* 操作ボタンのホバー */
.custom-confirm-drawer-menu_cancel:hover,
.custom-confirm-drawer-menu_submit:hover{
opacity:.8;
}
/* 左から表示するアニメーション */
@keyframes custom-confirm-drawer-menu_slide{
from{
opacity:0;
transform:translateX(-100%);
}
to{
opacity:1;
transform:translateX(0);
}
}
// 確認ダイアログを開くボタンを取得
const drawerOpen=document.querySelector('[data-drawer-open]');
// キャンセルボタンを取得
const drawerClose=document.querySelector('[data-drawer-close]');
// ドロワーメニュー背景を取得
const drawerOverlay=document.querySelector('[data-drawer-overlay]');
// 実行ボタンを取得
const drawerConfirm=document.querySelector('[data-drawer-confirm]');
// 実行結果メッセージを取得
const resultMessage=document.querySelector('[data-result-message]');
// 開くボタンをクリックしたら確認ダイアログを表示
drawerOpen.addEventListener('click',function(){
drawerOverlay.classList.add('is-active');
});
// キャンセルボタンをクリックしたら確認ダイアログを閉じる
drawerClose.addEventListener('click',function(){
drawerOverlay.classList.remove('is-active');
resultMessage.textContent='キャンセルしました。';
});
// 実行ボタンをクリックしたら確認ダイアログを閉じる
drawerConfirm.addEventListener('click',function(){
drawerOverlay.classList.remove('is-active');
resultMessage.textContent='削除されました。';
});
// 背景部分をクリックしたら確認ダイアログを閉じる
drawerOverlay.addEventListener('click',function(event){
if(event.target===drawerOverlay){
drawerOverlay.classList.remove('is-active');
resultMessage.textContent='キャンセルしました。';
}
});
ファイル構成と設置方法
このサンプルでは、HTML・CSS・JavaScriptをそれぞれ別ファイルに分けて使用します。index.html、style.css、script.js を同じフォルダに配置してください。
sample/
├── index.html
├── style.css
└── script.js
index.html をブラウザで開くと、カスタム確認ダイアログをドロワーメニュー形式で表示できます。「実行する」をクリックすると「削除されました。」、「キャンセル」または背景部分をクリックすると「キャンセルしました。」と画面上に表示されます。実際の削除処理は含まれていないため、必要に応じてJavaScriptへ任意の処理を追加してください。
カスタム確認ダイアログをドロワーメニューで表示する方法【HTML/CSS/JavaScript・サンプルコード付き】
コメント